# 反转链表

function reverse(head) {
  let pre = null
  let cur = head
  let next

  while(cur) {
    next = cur.next
    cur.next = pre
    pre = cur
    cur = next
  }

  return pre
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
var reverse2 = function(head) {
  if (head == null || head.next == null) return head
  let last = reverse2(head.next)
  head.next.next = head
  head.next = null
  return last
}
1
2
3
4
5
6
7
最后更新时间: 4/20/2021, 10:54:05 AM