day04 链表part02

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

想不明白的时候, 画图会很直观。

写好操作的伪代码,按照伪代码写。

class Solution {
    public ListNode swapPairs(ListNode head) {
        if(head == null || head.next == null){
            // 0个或者1个, 直接返回
            return head;
        }
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode cur = dummy;
        while(cur.next != null && cur.next.next != null){
            // 要交换的2个节点不为空就执行 cur-> 1-> 2-> 3   ---->  cur-> 2->1->3
            // 暂存3
            ListNode temp = cur.next.next.next;
            ListNode two = cur.next.next;
            // 2-> 1
            cur.next.next.next = cur.next;
            // 1-> 3
            cur.next.next = temp;
            // cur 指向2
            cur.next = two;
            cur = cur.next.next;
        }
        return dummy.next;
        
    }
}

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

双指针,保持差距是N, 然后遍历到操作节点的前一个,方便操作。

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode cur = dummy;
        ListNode fast = dummy;
        int count = n;
        while(count > 0 && fast != null){
            // 先走count步
            fast = fast.next;
            count--;
        }
        if(count != 0){
            return dummy.next;
        }
        while(fast.next != null){
            //fast在链表尾,cur在倒数n+1; 方便操作
            cur = cur.next;
            fast = fast.next;
        }
        cur.next = cur.next.next;
        return dummy.next;
    }
}

160.链表相交

如果是相交的链表,则尾部是有重叠的。

需要对齐尾部, 然后遍历

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode dummyA = new ListNode(0);
        ListNode dummyB = new ListNode(0);
        dummyA.next = headA;
        dummyB.next = headB;
        ListNode curA = dummyA;
        ListNode curB = dummyB;
        int m = 0;
        int n = 0;
        while(dummyA.next != null){
            m++;
            dummyA = dummyA.next;
        }
        while(dummyB.next != null){
            n++;
            dummyB = dummyB.next;
        }
        // 调整,剩余节点数一致
        if(m > n){
            while(m - n > 0){
                curA = curA.next;
                m--;
            }
        }else{
            while(n - m > 0){
                curB = curB.next;
                n--;
            }
        }
        while(curA != null && curB != null){
            if(curA == curB){
                return curA;
            }
            curA = curA.next;
            curB = curB.next;
        }
        // 不想交
        return null;

    }
}

142.环形链表II 

快慢指针,如果有相等,则有环

慢指针和头节点,到成环处距离相等。

public class Solution {
    public ListNode detectCycle(ListNode head) {
        // 不使用dummy
        ListNode slow = head;
        ListNode fast = head;
        while(fast !=  null && fast.next != null){
            // 慢的走1步, 快的走2步
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast){
                // 如果相等则成环
                ListNode start = head;
                while(slow != start){
                    // 慢节点和其起始节点   到入环点距离一样
                    slow = slow.next;
                    start = start.next;
                }
                return start;
            }
        }
        //未成环
        return null;
    }
}

你可能感兴趣的:(day04 链表part02)