新建一个头节点,遍历链表将所有节点依次插入到新的头节点后面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
var reverseList = function (head) { let newHead = new ListNode(); let cur = head; while (cur) { const next = cur.next; cur.next = newHead.next; newHead.next = cur; cur = next; } return newHead.next; };
|