【代码随想录】24. 两两交换链表中的节点 19.删除链表的倒数第N个节点 面试题 02.07. 链表相交 142.环形链表II

24. 两两交换链表中的节点

leetcode: 24. 两两交换链表中的节点.

通过虚拟节点指定好头是什么

class Solution {
    public ListNode swapPairs(ListNode head) {
        if(head==null ||head.next == null){
            return head;
        }
        ListNode dummy = new ListNode(-1,head);
        ListNode temp1 = null;
        ListNode temp2 = null;
        ListNode curr = dummy;
        while(curr.next!=null && curr.next.next!=null){
            temp1 = curr.next;
            temp2 = curr.next.next.next;
            curr.next = curr.next.next;
            temp1.next = temp2;
            curr.next.next = temp1;
            curr = curr.next.next;
        }
        return dummy.next;
    }
}

19.删除链表的倒数第N个节点

leetcode: 19.删除链表的倒数第N个节点.

创建虚拟头节点防止报空指针

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        //涉及链表一般使用快慢指针进行操作
        ListNode dummy = new ListNode(-1,head);
        ListNode fast = dummy;
        ListNode slow = dummy;
        for(int i = 0;i<=n;i++){
            fast = fast.next;
        }
        while(fast!=null){
            fast = fast.next;
            slow = slow.next;
        }
        if(slow.next!=null){
            slow.next =slow.next.next;
        }
        return dummy.next;
    }
}

面试题 02.07. 链表相交

面试题 02.07. 链表相交.

核心思想是比较两个头结点,同时要注意判断空值

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        //判断两个链表相交主要依靠指针进行遍历
        int lenA = 0;
        ListNode cur = headA;
        if(headA==null ||headB==null){
            return null;
        }
        //判断a链表的长度
        while(cur.next!=null){
            cur = cur.next;
            lenA++;
        }
        int lenB = 0;
        cur = headB;
        //判断b链表的长度
        while(cur.next!=null){
            cur = cur.next;
            lenB++;
        }  
        ListNode curA = headA;
        ListNode curB = headB;
        //根据差值推进指针      
        for(int i=0;i<Math.abs(lenA-lenB);i++){
            if(lenA>lenB){
                curA=curA.next;
            }else{
                curB=curB.next;
            }    
        }
        //当两边指针推进到同一长度,开始计算是否相同
        while(curA!=null){
            if(curA==curB){
                return curA;
            }else{
                curA= curA.next;
                curB=curB.next;
            }
        }
        return null;
    }
}

142.环形链表II

leetcode: 142.环形链表II.

使用快慢指针模拟前进时,慢指针在节点前前进了x,继续走了距离y相遇快指针,剩余距离z到节点,快指针前进了距离x+环长度a的倍数+y,快指针走了双倍慢指针的长度,所以可得:
2x+2y = x+na+y,即为x+y=n(y+z),x=(n-1)y+nz;这里n最大为2,(因为每次步进2,最坏的情况应该是刚好越过没相遇),所以应分两种情况来看。n=1,x=z;n=2,x=y+2z。y+z为一整圈,所以从相遇的地方让一个指针往节点走,和从起点走的一个指针以相同的速度,一定会在节点相遇。

public class Solution {
    public ListNode detectCycle(ListNode head) {
        //如果一个链表有环,那么快指针推进时会与慢指针相遇
        if(head==null||head.next==null){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        //快指针后面如果为空即停止
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if(fast==slow){
                //需要两个节点分别记录头结点和相遇节点
                ListNode x = head;
                ListNode y = fast;
                while(x!=y){
                    x=x.next;
                    y=y.next;
                }
                return x;
            }
        }
        return null;
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值