Topic describes

Their thinking

  1. Takes a pointer to the current node, starting with head.
  2. Another pointer is used to point to the previous node (pre) of the current node, which starts with null.
  3. Whenever we change the pointer to the current pointer, we must save the next node of the current pointer.

The problem solving code

var reverseList = function(head) {
    let cur = head;
    let pre = null;

    while (cur) {
        // First save the next node of the current node
        let temp = cur.next;
        cur.next = pre;
        pre = cur;
        cur = temp;
    }
    return pre;
};
Copy the code

The title to reflect

  • Learn how to use double Pointers to solve inverse linked lists.
  • This is a good way to solve the problem of reversing the linked list. It is necessary to save the next node of the current pointer when modifying the pointer.