Day 4 | 24. Swap Nodes in Pairs| 19. Remove Nth Node From End of List| 160.Intersection of Two Lists

Day 1 | 704. Binary Search | 27. Remove Element | 35. Search Insert Position | 34. First and Last Position of Element in Sorted Array
Day 2 | 977. Squares of a Sorted Array | 209. Minimum Size Subarray Sum | 59. Spiral Matrix II
Day 3 | 203. Remove Linked List Elements | 707. Design Linked List | 206. Reverse Linked List

Directory

  • LeetCode 24. Swap Nodes in Pairs
  • LeetCode 19. Remove Nth Node From End of List
  • LeetCode 160. Intersection of Two Linked Lists
  • LeetCode 142. Linked List Cycle II


LeetCode 24. Swap Nodes in Pairs

Question Link

Solution:

/**
 * 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, head);
        ListNode pre = dummy;
        ListNode temp = null;
        while(pre.next != null && pre.next.next!=null){
            pre.next = head.next;
            temp = head.next.next;
            head.next.next = head;
            head.next = temp;
            pre = head;
            head = head.next;
        }
        return dummy.next;
    }
}

Thoughts:

  • According to the following image, we successfully change the positions of a and b: Day 4 | 24. Swap Nodes in Pairs| 19. Remove Nth Node From End of List| 160.Intersection of Two Lists_第1张图片
    • define a dummy head p
    • let p point to b
    • let b point to a
    • let a point to 3
  • Then let p become a and let a become the next node of a, repeat the above procedures.
    Day 4 | 24. Swap Nodes in Pairs| 19. Remove Nth Node From End of List| 160.Intersection of Two Lists_第2张图片

LeetCode 19. Remove Nth Node From End of List

Question Link

Solution:

/**
 * 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 dummy = new ListNode(0, head);
        ListNode fast = dummy;
        ListNode slow = dummy;
        // As long as there is a difference of n nodes between the fast and slow pointers.
        for(int i = 0; i < n; i++)
            fast = fast.next;
        
        while(fast.next!=null){
            fast = fast.next;
            slow = slow.next;
        }
        // At this time, the `slow` pointer's position is the previous position of the element to be deleted.
        slow.next = slow.next.next;
        return dummy.next;
    }
}

Thoughts:

  • Let the fast node move forward n steps.
  • Let fast and slow move on them together until the fast node arrives at the end of the linked list.
  • At this time, the slow pointer’s position is the previous position of the element to be deleted.
  • Finally, delete the next node of the slow node.

LeetCode 160. Intersection of Two Linked Lists

Question Link

Solution:

/**
 * 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) {
        ListNode curA = headA;
        ListNode curB = headB;
        int lenA = 0;
        int lenB = 0;
        // calculate the length of linkedlist A
        while(curA != null){
            curA = curA.next;
            lenA++;
        } 
        // calculate the length of linkedlist B
        while(curB != null){
            curB = curB.next;
            lenB++;
        }
        curA = headA;
        curB = headB;
        int gap = 0;
        // calculate the gap and move the pointer of the longer linked list until it aligns with the start position of the other linked list.
        if(lenA > lenB){
            gap = lenA - lenB;
            while(gap-- > 0)
                curA = curA.next;
        }
        else{
            gap = lenB - lenA;
            while(gap-- > 0)
                curB = curB.next;
        }
        while(curA !=null){
            if(curA == curB)
                return curA;
            curA = curA.next;
            curB = curB.next;
        }
        return null;
    }
}

Thoughts:

  • Calculate the length of two linked lists
  • Calculate the gap between the length of two linked lists.
  • Move the pointer of the longer linked list until it aligns with the start position of the other linked list.
  • Compare curA with curB. If they are identical, return the point of intersection. Otherwise, move on them forward.

LeetCode 142. Linked List Cycle II

Question Link

Solution:

/**
 * 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) {
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast){   // exist a ring
                ListNode index1 = slow;
                ListNode index2 = head;
                while(index1 != index2){
                    index1 = index1.next;
                    index2 = index2.next;
                }
                if(index1 == index2)
                    return index1;
            } 
            
        }
        return null;
    }
}

Thoughts:

  • We adopted Double Pointer Method
  • Determine whether there is a ring.
    • Define a slow pointer that moves forward one step one time and a fast pointer that moves forward two steps one time.
    • If they meet halfway, it demonstrates that there is a ring.
  • If there is a ring,how to find the entrance of the ring?
    • Suppose the number of nodes from the head node to the ring entry node is x, the number of the node from the ring entry node to the encountered node is y, the number of the node from the encountered node to the ring entry node is z
    • A pointer index1 starts from the head node, and a pointer index2 starts from the encountered node. Each of these pointers moves forward one step at one time. When they encounter that is the ring entrance node.

你可能感兴趣的:(LeetCode,leetcode,链表)