-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path206. 反转链表.js
76 lines (61 loc) · 1.4 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// 206. 反转链表
// 简单
// 2.8K
// 相关企业
// 给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
// 示例 1:
// 输入:head = [1,2,3,4,5]
// 输出:[5,4,3,2,1]
// 示例 2:
// 输入:head = [1,2]
// 输出:[2,1]
// 示例 3:
// 输入:head = []
// 输出:[]
/**
* 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) {
// 在遍历链表时,将当前节点的 next 指针改为指向前一个节点
if (!head) {
return head
}
let before = null
let cur = head
while (cur) {
const next = cur.next
cur.next = before
before = cur
cur = next
}
return before
};
/**
* 方法一:递归
* @param {ListNode} head
* @return {ListNode}
*/
var reverseList = function(head) {
// 在遍历链表时,将当前节点的 next 指针改为指向前一个节点
if (!head) {
return head
}
const newHead = reverseList(head.next)
// 将下一个节点指向前一个节点
head.next.next = head
/**
* 打断当前节点与之前下一个节点的联系
* 例如: 4 -> 3 改为 3 -> 4, 并将4 -> 3 这个链打断
*/
head.next = null
return newHead
};