MCPcopy Create free account
hub / github.com/HuberTRoy/leetCode / reverseList

Function reverseList

Array/ReverseLinkedList.js:19–59  ·  view source on GitHub ↗
(head)

Source from the content-addressed store, hash-verified

17// 直至空。
18// PS,链表操作好麻烦。
19 var reverseList = function(head) {
20
21 let newHead = null
22
23 // 先不判断进行一次交换
24 let nHead = head
25 if (!nHead) {
26 return null
27 }
28 let next = nHead.next
29 if (!next) {
30 return nHead
31 }
32 let lNext = next.next
33
34 next.next = nHead
35 nHead.next = null
36
37 newHead = next
38
39 while (nHead && next && next.next) {
40 nHead = lNext
41 if (!nHead) {
42 return newHead
43 }
44
45 next = nHead.next
46
47 if (!next) {
48 nHead.next = newHead
49 return nHead
50 }
51 lNext = next.next
52
53 next.next = nHead
54 nHead.next = newHead
55
56 newHead = next
57
58 }
59};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected