1. Print the list from end to end

/*function ListNode(x){ this.val = x; this.next = null; } * /
function printListFromTailToHead(head)
{
    // write code here
    const ans = [];
    while(head) {
        ans.unshift(head.val);
        head = head.next;
    }
    return ans;
}
Copy the code

2. Reverse the linked list

/*function ListNode(x){ this.val = x; this.next = null; } * /
function ReverseList(pHead)
{
    // write code here
    if(! pHead || ! pHead.next)return pHead;
    let a = pHead;
    let b = pHead.next;
    while(b) {
        let c = b.next;
        b.next = a;
        a = b;
        b = c;
    }
    pHead.next = null;
    return a;
}
Copy the code

3. Replication of complex linked lists

/*function RandomListNode(x){ this.label = x; this.next = null; this.random = null; } * /
function Clone(pHead)
{
    // write code here
    if(! pHead)return null;
    let map = new Map(a); map.set(null.null);
    let node = pHead;
    while(node) {
        map.set(node, new RandomListNode(node.label));
        node = node.next;
    }
    // Copy the pointer
    node = pHead;
    while(node) {
        map.get(node).next = map.get(node.next);
        map.get(node).random = map.get(node.random);
        node = node.next;
    }
    return map.get(pHead);
    
}
Copy the code

4. Merge two sorted lists

/*function ListNode(x){ this.val = x; this.next = null; } * /
function Merge(l1, l2)
{
    // write code here
    if(! l1)return l2;
    if(! l2)return l1;
    let dummy = new ListNode(-1);
    let cur = dummy;
    while(l1 && l2) {
        if(l1.val < l2.val) {
            cur.next = new ListNode(l1.val);
            l1 = l1.next;
            cur = cur.next;
        }else {
            cur.next = newListNode(l2.val); l2 = l2.next; cur = cur.next; }}if(l1) cur.next = l1;
    if(l2) cur.next = l2;
    return dummy.next;
}
Copy the code

5. The KTH node from the bottom of the list

/** * function ListNode(val) { * this.val = val; * this.next = null; *} * /
var findKthToTail = function(pListHead, k) {
    // Find the length of the list first
    let cur = pListHead;
    let n = 0;
    while(cur){
        n++;
        cur = cur.next;
    }
    
    // The penultimate KTH node takes n-k steps
    cur = pListHead;
    if(k > n) return null;
    for(let i = 0; i < n - k; i ++){
        cur = cur.next;
    }
    return cur;
};
Copy the code

6. The entry to the central of the list

/** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; *} * /

/ * * *@param {ListNode} head
 * @return {ListNode}* /
var entryNodeOfLoop= function(head) {
    if(! head || ! head.next)return null;
    let p1 = head.next;
    let p2 = head.next.next;
    //1. Check whether there is a ring
    while(p1 ! =p2){if(p2 === null || p2.next === null) return null;
        p1 = p1.next;
        p2 = p2.next.next;
    }
    
    //2
    let length = 1;
    let tmp = p1;
    p1 = p1.next;
    while(p1 ! = tmp){ p1 = p1.next; length ++; }/ / 3. The entry point
    p1 = p2 = head;
    for(let i = 0; i < length; i ++){
        p1 = p1.next;
    }
    
    while(p1 ! = p2 ){ p1 = p1.next; p2 = p2.next; }return p1;
};
Copy the code

7. Common nodes for two linked lists

/*function ListNode(x){ this.val = x; this.next = null; } * /
function FindFirstCommonNode(pHead1, pHead2)
{
    // write code here
    let p = pHead1;
    let q = pHead2;
    while( p ! = q ){if(p) p = p.next;
        else p = pHead2;
        if(q) q = q.next;
        else q = pHead1;
    }
    return p;
}
Copy the code

8. Delete duplicate nodes

/*function ListNode(x){ this.val = x; this.next = null; } * /
function deleteDuplication(pHead)
{
    // write code here
    let dummy = new ListNode(-1);
    dummy.next = pHead;
    
    let p = dummy;
    while(p.next){
        let q = p.next; 
        while(q && p.next.val === q.val) q = q.next; // Find the unequal number
        // Check whether the length is 1
        if(p.next.next === q) p = p.next;
        else p.next = q;
    }
    return dummy.next;
}
Copy the code

9. Joseph’s question

function LastRemaining_Solution(n, m)
{
    // write code here
    if(n === 1) return 0;
    else return (LastRemaining_Solution(n-1, m) + m) % n;
}
Copy the code