-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0206. Reverse Linked List.js
213 lines (200 loc) · 3.88 KB
/
0206. Reverse Linked List.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Reverse a singly linked list.
// Example:
// Input: 1->2->3->4->5->NULL
// Output: 5->4->3->2->1->NULL
// Follow up:
// A linked list can be reversed either iteratively or recursively. Could you implement both?
// 1) 递归
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
function ListNode(val) {
this.val = val
this.next = null
}
const reverseList = (head) => {
if (!head) {
return null
}
let pre = head
let cur = pre.next
while (cur && cur.next) {
pre = pre.next
cur = pre.next
}
pre.next = null
if (cur) {
cur.next = reverseList(head)
}
if (cur && cur.next) {
return cur
} else {
return pre
}
}
// Runtime: 100 ms, faster than 6.36% of JavaScript online submissions for Reverse Linked List.
// Memory Usage: 35.8 MB, less than 15.22% of JavaScript online submissions for Reverse Linked List.
// Test case:
// let head1 = {
// val: 1,
// next: {
// val: 2,
// next: {
// val: 3,
// next: {
// val: 4,
// next: {
// val: 5,
// next: null
// }
// }
// }
// }
// }
// let head2 = {
// val: 1,
// next: null
// }
// let head3 = {
// val: 1,
// next: {
// val: 2,
// next: null
// }
// }
// let head4 = null
// console.log(reverseList(head1))
// console.log(reverseList(head2))
// console.log(reverseList(head3))
// console.log(reverseList(head4))
// 2) 递归
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
function ListNode(val) {
this.val = val
this.next = null
}
const reverseList = (head) => {
function reverseListInt (head, newHead) {
if (!head) {
return newHead
}
let next = head.next
head.next = newHead
return reverseListInt(next, head)
}
return reverseListInt(head, null)
}
// Runtime: 56 ms, faster than 83.91% of JavaScript online submissions for Reverse Linked List.
// Memory Usage: 35.4 MB, less than 28.26% of JavaScript online submissions for Reverse Linked List.
// Test case:
// let head1 = {
// val: 1,
// next: {
// val: 2,
// next: {
// val: 3,
// next: {
// val: 4,
// next: {
// val: 5,
// next: null
// }
// }
// }
// }
// }
// let head2 = {
// val: 1,
// next: null
// }
// let head3 = {
// val: 1,
// next: {
// val: 2,
// next: null
// }
// }
// let head4 = null
// console.log(reverseList(head1))
// console.log(reverseList(head2))
// console.log(reverseList(head3))
// console.log(reverseList(head4))
// 3) 迭代
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
function ListNode(val) {
this.val = val
this.next = null
}
const reverseList = (head) => {
let newHead = null
while (head) {
let next = head.next
head.next = newHead
newHead = head
head = next
}
return newHead
}
// Runtime: 52 ms, faster than 95.01% of JavaScript online submissions for Reverse Linked List.
// Memory Usage: 35 MB, less than 58.70% of JavaScript online submissions for Reverse Linked List.
// Test case:
// let head1 = {
// val: 1,
// next: {
// val: 2,
// next: {
// val: 3,
// next: {
// val: 4,
// next: {
// val: 5,
// next: null
// }
// }
// }
// }
// }
// let head2 = {
// val: 1,
// next: null
// }
// let head3 = {
// val: 1,
// next: {
// val: 2,
// next: null
// }
// }
// let head4 = null
// console.log(reverseList(head1))
// console.log(reverseList(head2))
// console.log(reverseList(head3))
// console.log(reverseList(head4))