Original link: leetcode-cn.com/problems/re…
Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL
We need to assign node 1 to the new list while traversing the old list, and then next of node 2 points to node 1 in the new list.
/** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; } */ /** * @param {ListNode} head * @return {ListNode} */ var reverseList = function (head) reversedNode = null; // Let originalNode = head; While (originalNode) {const tempNode = originalNode.next; OriginalNode. Next = reversedNode; originalNode. ReversedNode = originalNode; OriginalNode = tempNode; originalNode = tempNode; } return reversedNode; };Copy the code