-
Notifications
You must be signed in to change notification settings - Fork 65
/
206.反转链表.js
51 lines (49 loc) · 990 Bytes
/
206.反转链表.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
/*
* @lc app=leetcode.cn id=206 lang=javascript
*
* [206] 反转链表
*/
// @lc code=start
/**
* 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}
*
* prev cur next
*
* while {
*
* }
*/
var reverseList = function(head) {
// let prev = null;
// let curr = head;
// let next = null;
// while (curr !== null) {
// next = curr.next;
// curr.next = prev;
// prev = curr;
// curr = next;
// }
// return prev;
// next = curr.next
// curr.next = prev
// prev = curr
// curr = next
let [prev, current] = [null, head]
while (current) {
[current.next, prev, current] = [prev, current, current.next]
}
return prev
};
// @lc code=end
// 1. next = cur.next
// 2. cur.next = prev
// 3. prev = cur
// 4. cur = next