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


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: 在这里插入图片描述
    • 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.
    在这里插入图片描述

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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值