Linked list of LeetCode brushing diary

1. Exchange the nodes in the linked list in pairs

topic description

insert image description here

problem solving ideas

insert image description here
First save the next node of the current node and the next next node (that is, 1 and 3 nodes), and then point the next node of the current node to the 2 node, the next node of the 2 node points to the 1 node, and the next node of the 1 node Node points to 3 nodes

var swapPairs = function(head) {
     let preHead = new ListNode() // 虚拟头节点

     preHead.next = head;
     let cur = preHead;
     while(cur.next&&cur.next.next){ // 保证cur节点之后一定有两个节点,如果只有一个,说明只剩一个了 不操作

       let temp1= cur.next; // 纪录第一个节点
       let temp2 = cur.next.next.next; // 记录下一轮的第一个节点

       cur.next = cur.next.next;   // 当前节点到2节点
       cur.next.next = temp1;  // 2节点到1节点索引
       cur.next.next.next = temp2  // 1节点到3节点

       cur = cur.next.next // 当前节点后移两位

     }
    
    return preHead.next

};

2. Delete the last N node of the linked list

topic description

insert image description here

problem solving ideas

Thinking: To delete a node, you need to start from the previous node of the current node, and let the next node directly point to the next node, which is equivalent to deleting the current node. This problem can be solved by using double pointers. Let us assume that the length of the linked list is k, then let the fast pointer go n+1 steps first, and then let the fast and slow pointers move backward at the same time. When the fast pointer reaches the end of the linked list, the moving distance of the slow pointer is, kn-1, the next node is just the last nth node. So you can simply write the code:

var removeNthFromEnd = function(head, n) {
    let preHead = new ListNode(0,head)
   let fast = slow = preHead
   while(n -- && fast!==null){ // n -- 是先取值再计算,while执行 n + 1次
       fast = fast.next
   }
   while(fast&&fast.next){
       slow =slow.next
       fast = fast.next
   }
   slow.next = slow.next.next
   return preHead.next
};

3. Linked list intersect

topic description

insert image description here

problem solving ideas

The idea of ​​solving this problem is very ingenious. It can be analyzed from mathematics. Suppose there is an intersection point between two linked lists. The two linked lists go back from the beginning at the same time. When the A linked list reaches the end, let it point to the head node of the B linked list. After the B linked list goes to the end, it also points to the head node of the A linked list.
(The path length traveled by A = PA+PC+PB, the length of the path traveled by B is LB = PB + PC + PB , LA == LB)
···
var getIntersectionNode = function(headA, headB) { let pA = new ListNode(0,headA); let pB = new ListNode(0,headB)

while(pA!=pB){
    pA = pA ? pA.next:headB
    pB = pB?pB.next:headA;
}

return pA

};
···

4 circular linked list 2

topic description

insert image description here

problem solving ideas

This question can actually be implemented using a hash table. Starting from the head node, add the value of each node into the hash table. When a node appears for the second time, the node must be the starting point of the circular linked list.

var detectCycle = function(head) {
    // hash表
    let set = new Set()
    while(head){
        if(set.has(head)) {
            return head
        } 
           set.add(head)
           head = head.next;
        
    }
    return null

}

In addition, double pointers can also be used. The fast pointer takes two steps at a time, and the slow pointer takes one step at a time. The fast and slow pointers will meet in the ring. Using mathematical derivation, we can know that the distance from the meeting node to the starting point of the ring is the starting node The distance to the start of the ring.

let fast = head
    let slow = head;
    while(fast != null && fast.next != null){ // 没有环会直接退出
        slow = slow.next; // 快慢指针 如果有环则一定会在环内相遇 数学推导
        fast = fast.next.next
        if(slow === fast) { // 快慢指针相遇 数学推导  从相遇节点到开始到 环起点和 头节点到起点的节点数一致
           let index1 = head;
           let index2 = slow;
           while(index1 != index2){
              index1 = index1.next
              index2 = index2.next
              
           }
           return index1
        }
    }
    return null

Guess you like

Origin blog.csdn.net/Salange1/article/details/128269858