code

var reverseList = function(head) {
  let p1 = head
  let p2 = null 
  while (p1) {
    const tmp = p1.next
    p1.next = p2
    p2 = p1 
    p1 = tmp
  }
  return p2 
};
Copy the code

Train of thought

Time/space complexity

The code has a single body of a while loop with time complexity O(n). A temporary variable is a single value, an infinite array of object matrices, and the space complexity is O(1).