第四天:● 24. 两两交换链表中的节点 ● 19.删除链表的倒数第N个节点 ● 面试题 02.07. 链表相交 ● 142.环形链表II

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) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode curr = dummy;
        //add dummy node before head, and save the 0(crur.next) and 3(curr.next.next.next), and then connect
        // and then curr pointor move 2 postion.
        //头节点前边加dummy node,然后保存头节点(crur.next)跟index2(curr.next.next.next),交换后指针往后移动两位
        //一开始想复杂了,不需要两个指针,也不需要用bealoon记录是否移动过了。
        while(curr.next != null && curr.next.next != null){
            ListNode temp = curr.next;
            ListNode temp2 = curr.next.next.next;
            curr.next = curr.next.next;
            curr.next.next = temp;
            curr.next.next.next = temp2;
            curr = curr.next.next;
        }
        return dummy.next;
    }
}

 

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

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode fast = dummy;
        ListNode slow = dummy;
        //双指针,一快一慢,快的多走n+1步,刚好走到null, 慢的走到要删除的前一个。删除。
        //注意空指针异常
        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. 链表相交==160. Intersection of Two Linked Lists

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        //到达统一长度后,一起往后走直到有相同的。如果没有也是return null,都是A or b的其中一个。
        //reference: https://www.youtube.com/watch?v=1bWqD_MwWuw
        if(headA == null || headB == null){
            return null;
        }
        int lenA = len(headA);
        int lenB = len(headB);

        if(lenA > lenB){
            while(lenA != lenB){
                headA = headA.next;
                lenA--;
            }
        }else{
            while(lenA != lenB){
                headB = headB.next;
                lenB--;
                }
        }
        while(headA != headB){
            headA = headA.next;
            headB = headB.next;
        }
        return headA;  
}
public int len(ListNode list){
        int len = 0;
        while(list != null){
            list = list.next;
            len++;
        }
        return len;
        }
}

142. Linked List Cycle II

public class Solution {
    public ListNode detectCycle(ListNode head) {
        //https://www.youtube.com/watch?v=ZJDClARjgDI
        //https://programmercarl.com/0142.%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8II.html#%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E7%89%88%E6%9C%AC
        ListNode fast = head;
        ListNode slow = head;
        while(fast.next != null && fast.next.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(slow == fast){
                ListNode index1 = head;
                ListNode index2 = fast;
                while(index1 != index2){
                    index1 = index1.next;
                    index2 = index2.next;
                }
                return index1;
            }
        }
        return null;
    }
}

 

posted @ 2024-08-18 15:36  爱刷题的小盒子  阅读(140)  评论(0编辑  收藏  举报