-
Notifications
You must be signed in to change notification settings - Fork 422
/
Copy pathReverseLinkedList.js
59 lines (48 loc) · 1.13 KB
/
ReverseLinkedList.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* 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}
*/
// 翻转链表的思路
// 把head取出来,把head.next取出来,head.next.next也取出来。
// head.next = head
// 新的(head.next和head)与head.next.next进行新一轮交换。
// 直至空。
// PS,链表操作好麻烦。
var reverseList = function(head) {
let newHead = null
// 先不判断进行一次交换
let nHead = head
if (!nHead) {
return null
}
let next = nHead.next
if (!next) {
return nHead
}
let lNext = next.next
next.next = nHead
nHead.next = null
newHead = next
while (nHead && next && next.next) {
nHead = lNext
if (!nHead) {
return newHead
}
next = nHead.next
if (!next) {
nHead.next = newHead
return nHead
}
lNext = next.next
next.next = nHead
nHead.next = newHead
newHead = next
}
};