反转链表

class ListNode { constructor(val, next) { this.val = val; this.next = (next === undefined ? null : next); }}let node5 = new ListNode(5, null);let node4 = new ListNode(4, node5);let node3 = new ListNode(3, node4);let node2 = new ListNode(2, node3);let node1 = new ListNode(1, node2);var reverseList = function(head) { if(head == null || head.next == null) return head; var last = reverseList(head.next); head.next.next = head; head.next = null; return last;};var p = reverseList(node1);var ans = [];while(p != null) { ans.push(p.val); p = p.next;}console.log(ans);
