Java链表应用

Java链表应用链表类publicclassListNode{intval;ListNodenext=null;ListNode(intval){this.val=val;}}1、返回倒数第k个结点前后指针法需要考虑k大于链表长度的情况publicclassSolution{publicL…

大家好,又见面了,我是你们的朋友全栈君。

链表类

public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}

1、返回倒数第k个结点

前后指针法

需要考虑k大于链表长度的情况

public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
        if(k <= 0) {
        	return null;
        }
        ListNode pre = head;
        ListNode cur = head;
        for(int i = 0; i <k -1; i++) {
        	if(cur == null) {
        		return null;//处理k大于链表长度的情况
        	}
        	cur = cur.next;
        }
        if(cur == null) {
        	return null;
        }
        while(cur.next != null) {
        	pre = pre.next;//前后指针
        	cur = cur.next;
        }
        return pre;
    }
}

2、返回两个无环链表的交点

public class Solution2 {
	
	public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
		 int length1 = getListLength(pHead1);
		 int length2 = getListLength(pHead2);
		 int lengthD = length1 - length2;
		 ListNode pLong = pHead1;
		 ListNode pShort = pHead2;
		 if(length1 < length2) {
			 pLong = pHead2;
			 pShort = pHead1;
			 lengthD = -lengthD;
		 }
		 //长链表先走lengthD的长度
		 for(int i = 0; i < lengthD; i++) {
			 pLong = pLong.next;
		 }
		 //两个链表同步走,值相等时停止返回
		 while(pLong != null && pShort != null && pLong != pShort) {
			 pLong = pLong.next;
			 pShort = pShort.next;
		 }
		 return pShort;
    }

	private int getListLength(ListNode pHead1) {
		int length = 0;
		ListNode node = pHead1;
		while(node != null) {
			length++;
			node = node.next;
		}
		return length;
	}
}

3、删除排序链表的重复结点,重复结点不保留 

public class Solution3 {
	public ListNode deleteDuplication(ListNode pHead){
		if(pHead == null || pHead.next == null) {
			return pHead;
		}
		
		if(pHead.val == pHead.next.val) {
			ListNode aft = pHead.next;
			while(aft != null && aft.val == pHead.val) {//跳过所有相同的值,找到第一个不同的值
				aft = aft.next;
			}
			return deleteDuplication(aft);//从第一个不同的值继续递归
		}
		else {
			//当前没有重复元素,就从下一个节点开始
			pHead.next = deleteDuplication(pHead.next);
			return pHead;
		}
    }
}

4、输入一个链表,按链表值从尾到头的顺序返回一个ArrayList

将该链表的值保存在栈中

新建链表,添加栈pop的值

public class Solution4 {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        Stack<Integer> stack = new Stack<>();
        while(listNode != null){
            stack.push(listNode.val);
            listNode = listNode.next;
        }
        ArrayList<Integer> arrayList = new ArrayList<>();
        while(!stack.isEmpty()){
            arrayList.add(stack.pop());
        }
        return arrayList;
    }
}

5、右旋单链表,使每个结点向右移动n个位置,n为非负数

public class Solution5 {
	public ListNode rotateRight(ListNode head, int n) {
	    if(head == null || head.next == null || n == 0){
	        return head;
	    }
	 
	    int len = getLength(head);
	    if(n % len == 0) {
	    	return head;
	    }else {
	    	n %= len;
	    }
	    
	    ListNode mid = head;
	    ListNode pre = head;
	    for(int i = 0; i < len - n; i++){
	        pre = mid;
	        mid = mid.next;
	    }
	    pre.next = null;
	    ListNode newHead = mid;
	    while(mid.next != null){
	        mid = mid.next;
	    }
	    mid.next = head;
	    return newHead;
	}
	private int getLength(ListNode head){
	    ListNode node = head;
	    int len = 0;
	    while(node != null){
	        len++;
	        node = node.next;
	    }
	    return len;
	}
}

6、删除单链表中从后往前数的第n个结点

public class Solution6 {
	public ListNode removeNthFromEnd(ListNode head, int n) {
	    if(head == null){
	        return null;
	    }
	    
	    if(n <= 0){
	        return head;
	    }
	    
	    //前后指针
	    ListNode pre = head;
	    ListNode cur = head;
	    int i = 0;
	    for(; i < n && cur != null; i++){
	        cur = cur.next;
	    }
	    if(i < n) {//处理n大于链表长度的情况
	    	return head;
	    }
	    if(cur == null) {//头删
	    	head = head.next;
	    	return head;
	    }
	    
	    //cur与pre相差n-1个位置
	    //cur = null, pre在n+1处
        while(cur.next != null){
            pre = pre.next;
            cur = cur.next;
        }
        //删除pre
        pre.next = pre.next.next;
        
	    return head;
	}
}

7、反转链表

public class Solution7 {
    public ListNode ReverseList(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        
        //三个指针遍历
        ListNode pre = null;
        ListNode node = head;
        ListNode aft = head.next;
        while(node != null){
            node.next = pre;
            pre = node;
            node = aft;
            if(aft != null){
                aft = aft.next;
            }
            else{
                aft = null;
            }
        }
        return pre;
    }
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/142640.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • CentOS8快速部署轻量级自动化运维平台Spug

    CentOS8快速部署轻量级自动化运维平台SpugSpug面向中小型企业设计的轻量级无Agent的自动化运维平台,整合了主机管理、主机批量执行、主机在线终端、文件在线上传下载、应用发布部署、在线任务计划、配置中心、监控、报警等一系列功能。Spug的特性批量执行:主机命令在线批量执行在线终端:主机支持浏览器在线终端登录文件管理:主机文件在线上传下载任务计划:灵活的在线任务计划发布部署:支持自定义发布部署流程配置中心:支持KV、文本、json等格式的配置监控中心:支持站点、端口、进程、自定义等监控报警中心:支持短信、

    2022年5月16日
    44
  • spring + jdbc + extjs configuration

    spring + jdbc + extjs configuration

    2022年1月30日
    43
  • 流量分析基础篇

    流量分析基础篇流量分析1.流量分析是什么?  网络流量分析是指捕捉网络中流动的数据包,并通过查看包内部数据以及进行相关的协议、流量分析、统计等来发现网络运行过程中出现的问题。  CTF比赛中,通常比赛中会提供一个包含流量数据的PCAP文件,进行分析。2.数据包分析总体把握–协议分级–端点统计过滤赛选–过滤…

    2022年6月1日
    43
  • 汇编指令之移位指令[通俗易懂]

    汇编指令之移位指令[通俗易懂]   移位指令包括了算术移位指令、逻辑移位指令、循环移位指令。    格式为:xxxoper1,CL/1       ;移位次数只能是1或者存放在CL里面。一、算术移位指令1、算术左移指令SAL功能:左移一次,最低位补0,最高位送入CF标志位,如图:意义:左移n次,等于x2的n次幂。所以可用于有符号操作数做x2的n次幂运算。2、算术右移指令SAR功能:右移一次,最高位保持不变,最低位送入…

    2022年4月28日
    61
  • javascript中通过document.cookie写入不了cookie的问题「建议收藏」

    javascript中通过document.cookie写入不了cookie的问题「建议收藏」网上有很多通过document.cookie来写入cookie的写法,使用的代码如下但是在本地直接通过浏览器浏览的时候,并不能写入cookie。经过试验才发现,只有当用在服务器或者本地的服务器中的时候,才能使用这个方法写入cookie

    2022年7月20日
    50
  • VBA编程_常用函数总结1[通俗易懂]

    VBA编程_常用函数总结1[通俗易懂]文章目录IsEmptyReplaceAscMidRoundIsEmpty  用于判断单元格是否为空:SubMain()ActiveSheet.Cells(7,3).Value=1IfIsEmpty(ActiveSheet.Cells(7,3))ThenDebug.Print”IsEmpty”ElseDebug.Print”NotEmpty”EndIfEndSubReplace  函数原型如下

    2022年5月22日
    39

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号