23
23
24
24
## 解法
25
25
26
- 定义指针 ` pre ` , ` cur ` 分别指向 null 和头节点。
26
+ ** 方法一:头插法 **
27
27
28
- 遍历链表,将 ` cur.next ` 临时保存到 ` t ` 中,然后改变指针 ` cur ` 指向的节点的指向,将其指向 ` pre ` 指针指向的节点,即 ` cur .next = pre ` 。然后 ` pre ` 指针指向 ` cur ` , ` cur ` 指针往前走 。
28
+ 创建虚拟头节点 $dummy$,遍历链表,将每个节点依次插入 $dummy$ 的下一个节点。遍历结束,返回 $dummy .next$ 。
29
29
30
- 当遍历结束后,返回 ` pre ` 指针即可。
30
+ 时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为链表的长度。
31
+
32
+ ** 方法二:递归**
33
+
34
+ 递归反转链表的第二个节点到尾部的所有节点,然后 $head$ 插在反转后的链表的尾部。
35
+
36
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为链表的长度。
31
37
32
38
<!-- tabs:start -->
33
39
43
49
44
50
class Solution :
45
51
def reverseList (self , head : ListNode) -> ListNode:
46
- pre, cur = None , head
47
- while cur:
48
- t = cur.next
49
- cur.next = pre
50
- pre = cur
51
- cur = t
52
- return pre
52
+ dummy = ListNode()
53
+ curr = head
54
+ while curr:
55
+ next = curr.next
56
+ curr.next = dummy.next
57
+ dummy.next = curr
58
+ curr = next
59
+ return dummy.next
60
+ ```
61
+
62
+ ``` python
63
+ # Definition for singly-linked list.
64
+ # class ListNode:
65
+ # def __init__(self, x):
66
+ # self.val = x
67
+ # self.next = None
68
+
69
+ class Solution :
70
+ def reverseList (self , head : ListNode) -> ListNode:
71
+ if head is None or head.next is None :
72
+ return head
73
+ ans = self .reverseList(head.next)
74
+ head.next.next = head
75
+ head.next = None
76
+ return ans
53
77
```
54
78
55
79
### ** Java**
@@ -65,15 +89,130 @@ class Solution:
65
89
*/
66
90
class Solution {
67
91
public ListNode reverseList (ListNode head ) {
68
- ListNode pre = null , cur = head;
69
- while (cur != null ) {
70
- ListNode t = cur. next;
71
- cur. next = pre;
72
- pre = cur;
73
- cur = t;
92
+ ListNode dummy = new ListNode (0 );
93
+ ListNode curr = head;
94
+ while (curr != null ) {
95
+ ListNode next = curr. next;
96
+ curr. next = dummy. next;
97
+ dummy. next = curr;
98
+ curr = next;
99
+ }
100
+ return dummy. next;
101
+ }
102
+ }
103
+ ```
104
+
105
+ ``` java
106
+ /**
107
+ * Definition for singly-linked list.
108
+ * public class ListNode {
109
+ * int val;
110
+ * ListNode next;
111
+ * ListNode(int x) { val = x; }
112
+ * }
113
+ */
114
+ class Solution {
115
+ public ListNode reverseList (ListNode head ) {
116
+ if (head == null || head. next == null ) {
117
+ return head;
118
+ }
119
+ ListNode ans = reverseList(head. next);
120
+ head. next. next = head;
121
+ head. next = null ;
122
+ return ans;
123
+ }
124
+ }
125
+ ```
126
+
127
+ ### ** C++**
128
+
129
+ ``` cpp
130
+ /* *
131
+ * Definition for singly-linked list.
132
+ * struct ListNode {
133
+ * int val;
134
+ * ListNode *next;
135
+ * ListNode(int x) : val(x), next(NULL) {}
136
+ * };
137
+ */
138
+ class Solution {
139
+ public:
140
+ ListNode* reverseList(ListNode* head) {
141
+ ListNode* dummy = new ListNode(0);
142
+ ListNode* curr = head;
143
+ while (curr) {
144
+ ListNode* next = curr->next;
145
+ curr->next = dummy->next;
146
+ dummy->next = curr;
147
+ curr = next;
74
148
}
75
- return pre ;
149
+ return dummy->next ;
76
150
}
151
+ };
152
+ ```
153
+
154
+ ```cpp
155
+ /**
156
+ * Definition for singly-linked list.
157
+ * struct ListNode {
158
+ * int val;
159
+ * ListNode *next;
160
+ * ListNode(int x) : val(x), next(NULL) {}
161
+ * };
162
+ */
163
+ class Solution {
164
+ public:
165
+ ListNode* reverseList(ListNode* head) {
166
+ if (!head || !head->next) {
167
+ return head;
168
+ }
169
+ ListNode* ans = reverseList(head->next);
170
+ head->next->next = head;
171
+ head->next = nullptr;
172
+ return ans;
173
+ }
174
+ };
175
+ ```
176
+
177
+ ### ** Go**
178
+
179
+ ``` go
180
+ /* *
181
+ * Definition for singly-linked list.
182
+ * type ListNode struct {
183
+ * Val int
184
+ * Next *ListNode
185
+ * }
186
+ */
187
+ func reverseList (head *ListNode ) *ListNode {
188
+ dummy := &ListNode{}
189
+ curr := head
190
+ for curr != nil {
191
+ next := curr.Next
192
+ curr.Next = dummy.Next
193
+ dummy.Next = curr
194
+ curr = next
195
+ }
196
+ return dummy.Next
197
+ }
198
+ ```
199
+
200
+ ``` go
201
+ /* *
202
+ * Definition for singly-linked list.
203
+ * type ListNode struct {
204
+ * Val int
205
+ * Next *ListNode
206
+ * }
207
+ */
208
+ func reverseList (head *ListNode ) *ListNode {
209
+ if head == nil || head.Next == nil {
210
+ return head
211
+ }
212
+ ans := reverseList (head.Next )
213
+ head.Next .Next = head
214
+ head.Next = nil
215
+ return ans
77
216
}
78
217
```
79
218
@@ -92,69 +231,69 @@ class Solution {
92
231
* @return {ListNode}
93
232
*/
94
233
var reverseList = function (head ) {
95
- let node = head ;
96
- let pre = null ;
97
- while (node ) {
98
- let cur = node ;
99
- node = cur .next ;
100
- cur .next = pre ;
101
- pre = cur ;
234
+ const dummy = new ListNode ( 0 ) ;
235
+ let curr = head ;
236
+ while (curr ) {
237
+ const next = curr . next ;
238
+ curr . next = dummy .next ;
239
+ dummy .next = curr ;
240
+ curr = next ;
102
241
}
103
- return pre ;
242
+ return dummy . next ;
104
243
};
105
244
```
106
245
107
- ### ** Go**
108
-
109
- ``` go
110
- func reverseList (head *ListNode ) *ListNode {
111
- if head == nil ||head.Next == nil {
112
- return head
113
- }
114
- dummyHead := &ListNode{}
115
- cur := head
116
- for cur != nil {
117
- tmp := cur.Next
118
- cur.Next = dummyHead.Next
119
- dummyHead.Next = cur
120
- cur = tmp
246
+ ``` js
247
+ /**
248
+ * Definition for singly-linked list.
249
+ * function ListNode(val) {
250
+ * this.val = val;
251
+ * this.next = null;
252
+ * }
253
+ */
254
+ /**
255
+ * @param {ListNode} head
256
+ * @return {ListNode}
257
+ */
258
+ var reverseList = function (head ) {
259
+ if (! head || ! head .next ) {
260
+ return head;
121
261
}
122
- return dummyHead.Next
123
- }
262
+ const ans = reverseList (head .next );
263
+ head .next .next = head;
264
+ head .next = null ;
265
+ return ans;
266
+ };
124
267
```
125
268
126
- ### ** C++ **
269
+ ### ** TypeScript **
127
270
128
- ``` cpp
129
- class Solution {
130
- public:
131
- ListNode* reverseList(ListNode* head) {
132
- // 通过头插实现逆序
133
- // ListNode * first = new ListNode(-1);
134
- // ListNode * p = head, * q;
135
- // while (p) {
136
- // q = p->next;
137
- // p->next = first->next;
138
- // first->next = p;
139
- // p = q;
140
- // }
141
- // return first->next;
142
-
143
- // 常规方法
144
- ListNode *pre = NULL, *cur = head;
145
- while (cur) {
146
- ListNode* temp = cur->next;
147
- cur->next = pre;
148
- pre = cur;
149
- cur = temp;
150
- }
151
- return pre;
271
+ ``` ts
272
+ /**
273
+ * Definition for singly-linked list.
274
+ * class ListNode {
275
+ * val: number
276
+ * next: ListNode | null
277
+ * constructor(val?: number, next?: ListNode | null) {
278
+ * this.val = (val===undefined ? 0 : val)
279
+ * this.next = (next===undefined ? null : next)
280
+ * }
281
+ * }
282
+ */
283
+
284
+ function reverseList(head : ListNode | null ): ListNode | null {
285
+ const dummy = new ListNode (0 );
286
+ let curr = head ;
287
+ while (curr ) {
288
+ const next = curr .next ;
289
+ curr .next = dummy .next ;
290
+ dummy .next = curr ;
291
+ curr = next ;
152
292
}
153
- };
293
+ return dummy .next ;
294
+ }
154
295
```
155
296
156
- ### ** TypeScript**
157
-
158
297
``` ts
159
298
/**
160
299
* Definition for singly-linked list.
@@ -169,15 +308,13 @@ public:
169
308
*/
170
309
171
310
function reverseList(head : ListNode | null ): ListNode | null {
172
- let cur = head ;
173
- let pre = null ;
174
- while (cur != null ) {
175
- const temp = cur .next ;
176
- cur .next = pre ;
177
- pre = cur ;
178
- cur = temp ;
311
+ if (! head || ! head .next ) {
312
+ return head ;
179
313
}
180
- return pre ;
314
+ const ans = reverseList (head .next );
315
+ head .next .next = head ;
316
+ head .next = null ;
317
+ return ans ;
181
318
}
182
319
```
183
320
@@ -228,14 +365,37 @@ impl Solution {
228
365
*/
229
366
public class Solution {
230
367
public ListNode ReverseList (ListNode head ) {
231
- ListNode pre = null , cur = head ;
232
- while (cur != null ) {
233
- ListNode nxt = cur .next ;
234
- cur .next = pre ;
235
- pre = cur ;
236
- cur = nxt ;
368
+ ListNode dummy = new ListNode (0 );
369
+ ListNode curr = head ;
370
+ while (curr != null ) {
371
+ ListNode next = curr .next ;
372
+ curr .next = dummy .next ;
373
+ dummy .next = curr ;
374
+ curr = next ;
375
+ }
376
+ return dummy .next ;
377
+ }
378
+ }
379
+ ```
380
+
381
+ ``` cs
382
+ /**
383
+ * Definition for singly-linked list.
384
+ * public class ListNode {
385
+ * public int val;
386
+ * public ListNode next;
387
+ * public ListNode(int x) { val = x; }
388
+ * }
389
+ */
390
+ public class Solution {
391
+ public ListNode ReverseList (ListNode head ) {
392
+ if (head == null || head .next == null ) {
393
+ return head ;
237
394
}
238
- return pre ;
395
+ ListNode ans = ReverseList (head .next );
396
+ head .next .next = head ;
397
+ head .next = null ;
398
+ return ans ;
239
399
}
240
400
}
241
401
```
0 commit comments