LC 206. Reverse Linked List

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function (head) {
  if (head === null || head.next === null) return head
  const newHead = reverseList(head.next)
  head.next.next = head
  head.next = null
  return newHead
};





评论和交流请发送邮件到 me@tianhegao.com





通过以下渠道赞赏此文