代码随想录算法训练营第4天 | 链表两两交换、删除倒N、链表相交、环形链表

2024年7月6日

两两交换链表元素

继续用虚拟头节点,多建立几个记录指针。

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

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode swapPairs(ListNode head) {
        if(head==null){
            return null;
        }
        if(head.next==null){
            return head;
        }
        ListNode nextSpan = head.next.next;
        ListNode vir = new ListNode();
        vir.next = head;
        ListNode before = vir;
        while(true){
            ListNode cur1 = before.next;
            ListNode cur2 = cur1.next;
            before.next = cur2;
            cur2.next = cur1;
            cur1.next = nextSpan;
            before = cur1;
            cur1 = before.next;
            if(cur1==null||cur1.next==null){
                break;
            }
            cur2 = cur1.next;
            nextSpan = cur2.next;
        }
        return vir.next;
    }
}

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

双指针法
第二个指针先移动n+1个,当到末尾时,第一个指针的下一个就删除。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode vir = new ListNode();
        vir.next = head;
        ListNode fast = vir;
        ListNode slow = vir;
        for(int i=0;i<=n;i++){
            fast = fast.next;
        }
        while(fast!=null){
            slow = slow.next;
            fast = fast.next;
        }
        slow.next = slow.next.next;
        return vir.next;
    }
}

面试题 02.07. 链表相交

求出长度差值,然后长的切掉多出来的,然后依次比较节点是否相同。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA==null||headB==null){
            return null;
        }
        ListNode p1 = headA,p2=headB;
        int a=1,b=1;
        while(p1.next!=null){
            p1 = p1.next;
            a+=1;
        }
        while(p2.next!=null){
            p2=p2.next;
            b+=1;
        }
        p1 = headA;
        p2 = headB;
        int cha = Math.abs(a-b);
        if(a>b){
            for(int i=0;i<cha;i++){
                 headA = headA.next;
            }
        }else{
            for(int i=0;i<cha;i++){
                 headB = headB.next;
            }
        }
        p1 = headA;
        p2 = headB;
        while(p1!=null || p2!=null){
            if(p1==p2){
                return p1;
            }else{
                p1 = p1.next;
                p2 = p2.next;
            }
        }
        return null;
    }
}

环形链表

不需要虚拟头节点,快慢指针,相交之后,从相交点和head到入环位置的距离相同。

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode detectCycle(ListNode head) {
        if(head==null){
            return null;
        }
        ListNode fast=head,slow=head;
        do{
            fast=fast.next;
            if(fast==null){
                return null;
            }
            fast = fast.next;
            slow = slow.next;
            if(fast==null){
                return null;
            }
        }while(fast!=slow);
        fast = head;
        while(fast!=slow){
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }
}
posted @ 2024-07-06 15:23  hailicy  阅读(1)  评论(0编辑  收藏  举报