Topic describes

Define a function that takes the head node of a linked list, reverses the list, and outputs the head node of the reversed list.

Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL

/ How to solve the problem// 1, reverse the two nodes: point n+1 next to n
// 2, reverse multiple nodes: double pointer traverses the list, repeat the above operation

//
// 1, double pointer traverses the list
// 2. Reverse pointer
// 3. VX: mike_fss888(add friends wow 😝)

/ * * *@param {ListNode} head
 * @return {ListNode}* /
var reverseList = function (head) {
  let p1 = head
  let p2 = null
  while (p1) {
    const tmp = p1.next
    p1.next = p2
    p2 = p1
    p1 = p1.next
  }
};

// The title is reprinted from licou official website:
// https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/
Copy the code