44
44
45
45
<!-- 这里可写通用的实现逻辑 -->
46
46
47
- ** 方法一:迭代 **
47
+ ** 方法一:递归 **
48
48
49
- 设置虚拟头节点 dummy,pre 指针初始指向 dummy,遍历链表,每次交换 pre 后面的两个节点即可 。
49
+ 我们可以通过递归的方式实现两两交换链表中的节点 。
50
50
51
- 时间复杂度为 $O(n)$,空间复杂度为 $O(1)$,其中 $n$ 是链表的长度 。
51
+ 递归的终止条件是链表中没有节点,或者链表中只有一个节点,此时无法进行交换,直接返回该节点 。
52
52
53
- ** 方法二:递归 **
53
+ 否则,我们递归交换链表 $head.next.next$,记交换后的头节点为 $t$,然后我们记 $head$ 的下一个节点为 $p$,然后令 $p$ 指向 $head$,而 $head$ 指向 $t$,最后返回 $p$。
54
54
55
- 时间复杂度为 $O(n)$,空间复杂度为 $O(n)$,其中 $n$ 是链表的长度。
55
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 是链表的长度。
56
+
57
+ ** 方法二:迭代**
58
+
59
+ 我们设置一个虚拟头节点 $dummy$,初始时指向 $head$,然后设置两个指针 $pre$ 和 $cur$,初始时 $pre$ 指向 $dummy$,而 $cur$ 指向 $head$。
60
+
61
+ 接下来,我们遍历链表,每次需要交换 $pre$ 后面的两个节点,因此我们先判断 $cur$ 和 $cur.next$ 是否为空,若不为空,则进行交换,否则终止循环。
62
+
63
+ 时间复杂度 $O(n)$,空间复杂度 $O(1)$,其中 $n$ 是链表的长度。
56
64
57
65
<!-- tabs:start -->
58
66
67
75
# self.val = val
68
76
# self.next = next
69
77
class Solution :
70
- def swapPairs (self , head : ListNode) -> ListNode:
78
+ def swapPairs (self , head : Optional[ListNode]) -> Optional[ListNode]:
79
+ if head is None or head.next is None :
80
+ return head
81
+ t = self .swapPairs(head.next.next)
82
+ p = head.next
83
+ p.next = head
84
+ head.next = t
85
+ return p
86
+ ```
87
+
88
+ ``` python
89
+ # Definition for singly-linked list.
90
+ # class ListNode:
91
+ # def __init__(self, val=0, next=None):
92
+ # self.val = val
93
+ # self.next = next
94
+ class Solution :
95
+ def swapPairs (self , head : Optional[ListNode]) -> Optional[ListNode]:
71
96
dummy = ListNode(next = head)
72
97
pre, cur = dummy, head
73
98
while cur and cur.next:
@@ -83,6 +108,31 @@ class Solution:
83
108
84
109
<!-- 这里可写当前语言的特殊实现逻辑 -->
85
110
111
+ ``` java
112
+ /**
113
+ * Definition for singly-linked list.
114
+ * public class ListNode {
115
+ * int val;
116
+ * ListNode next;
117
+ * ListNode() {}
118
+ * ListNode(int val) { this.val = val; }
119
+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
120
+ * }
121
+ */
122
+ class Solution {
123
+ public ListNode swapPairs (ListNode head ) {
124
+ if (head == null || head. next == null ) {
125
+ return head;
126
+ }
127
+ ListNode t = swapPairs(head. next. next);
128
+ ListNode p = head. next;
129
+ p. next = head;
130
+ head. next = t;
131
+ return p;
132
+ }
133
+ }
134
+ ```
135
+
86
136
``` java
87
137
/**
88
138
* Definition for singly-linked list.
@@ -97,7 +147,8 @@ class Solution:
97
147
class Solution {
98
148
public ListNode swapPairs (ListNode head ) {
99
149
ListNode dummy = new ListNode (0 , head);
100
- ListNode pre = dummy, cur = head;
150
+ ListNode pre = dummy;
151
+ ListNode cur = head;
101
152
while (cur != null && cur. next != null ) {
102
153
ListNode t = cur. next;
103
154
cur. next = t. next;
@@ -111,38 +162,34 @@ class Solution {
111
162
}
112
163
```
113
164
114
- ### ** JavaScript **
165
+ ### ** C++ **
115
166
116
- ``` js
167
+ ``` cpp
117
168
/* *
118
169
* Definition for singly-linked list.
119
- * function ListNode(val, next) {
120
- * this.val = (val===undefined ? 0 : val)
121
- * this.next = (next===undefined ? null : next)
122
- * }
123
- */
124
- /**
125
- * @param {ListNode} head
126
- * @return {ListNode}
170
+ * struct ListNode {
171
+ * int val;
172
+ * ListNode *next;
173
+ * ListNode() : val(0), next(nullptr) {}
174
+ * ListNode(int x) : val(x), next(nullptr) {}
175
+ * ListNode(int x, ListNode *next) : val(x), next(next) {}
176
+ * };
127
177
*/
128
- var swapPairs = function ( head ) {
129
- const dummy = new ListNode ( 0 , head);
130
- let pre = dummy;
131
- let cur = head;
132
- while (cur && cur . next ) {
133
- const t = cur . next ;
134
- cur . next = t . next ;
135
- t . next = cur ;
136
- pre . next = t ;
137
- pre = cur ;
138
- cur = cur . next ;
178
+ class Solution {
179
+ public:
180
+ ListNode * swapPairs(ListNode * head) {
181
+ if (! head || !head->next) {
182
+ return head;
183
+ }
184
+ ListNode * t = swapPairs(head-> next->next) ;
185
+ ListNode * p = head->next ;
186
+ p-> next = head ;
187
+ head->next = t ;
188
+ return p ;
139
189
}
140
- return dummy .next ;
141
190
};
142
191
```
143
192
144
- ### ** C++**
145
-
146
193
```cpp
147
194
/**
148
195
* Definition for singly-linked list.
@@ -158,8 +205,9 @@ class Solution {
158
205
public:
159
206
ListNode* swapPairs(ListNode* head) {
160
207
ListNode* dummy = new ListNode(0, head);
161
- ListNode * pre = dummy, * cur = head;
162
- while (cur != nullptr && cur->next != nullptr) {
208
+ ListNode* pre = dummy;
209
+ ListNode* cur = head;
210
+ while (cur && cur->next) {
163
211
ListNode* t = cur->next;
164
212
cur->next = t->next;
165
213
t->next = cur;
@@ -174,7 +222,25 @@ public:
174
222
175
223
### ** Go**
176
224
177
- 迭代:
225
+ ``` go
226
+ /* *
227
+ * Definition for singly-linked list.
228
+ * type ListNode struct {
229
+ * Val int
230
+ * Next *ListNode
231
+ * }
232
+ */
233
+ func swapPairs (head *ListNode ) *ListNode {
234
+ if head == nil || head.Next == nil {
235
+ return head
236
+ }
237
+ t := swapPairs (head.Next .Next )
238
+ p := head.Next
239
+ p.Next = head
240
+ head.Next = t
241
+ return p
242
+ }
243
+ ```
178
244
179
245
``` go
180
246
/* *
@@ -185,41 +251,125 @@ public:
185
251
* }
186
252
*/
187
253
func swapPairs (head *ListNode ) *ListNode {
188
- dummy := &ListNode{0, head}
254
+ dummy := &ListNode{Next: head}
189
255
pre , cur := dummy, head
190
256
for cur != nil && cur.Next != nil {
191
257
t := cur.Next
192
258
cur.Next = t.Next
193
259
t.Next = cur
194
260
pre.Next = t
195
- pre = cur
196
- cur = cur.Next
261
+ pre, cur = cur, cur.Next
197
262
}
198
263
return dummy.Next
199
264
}
200
265
```
201
266
202
- 递归:
267
+ ### ** TypeScript **
203
268
204
- ``` go
269
+ ``` ts
205
270
/**
206
271
* Definition for singly-linked list.
207
- * type ListNode struct {
208
- * Val int
209
- * Next *ListNode
272
+ * class ListNode {
273
+ * val: number
274
+ * next: ListNode | null
275
+ * constructor(val?: number, next?: ListNode | null) {
276
+ * this.val = (val===undefined ? 0 : val)
277
+ * this.next = (next===undefined ? null : next)
278
+ * }
210
279
* }
211
280
*/
212
- func swapPairs (head *ListNode ) *ListNode {
213
- if head == nil || head.Next == nil {
214
- return head
215
- }
216
- res := swapPairs (head.Next .Next )
217
- p := head.Next
218
- p.Next , head.Next = head, res
219
- return p
281
+
282
+ function swapPairs(head : ListNode | null ): ListNode | null {
283
+ if (! head || ! head .next ) {
284
+ return head ;
285
+ }
286
+ const t = swapPairs (head .next .next );
287
+ const p = head .next ;
288
+ p .next = head ;
289
+ head .next = t ;
290
+ return p ;
291
+ }
292
+ ```
293
+
294
+ ``` ts
295
+ /**
296
+ * Definition for singly-linked list.
297
+ * class ListNode {
298
+ * val: number
299
+ * next: ListNode | null
300
+ * constructor(val?: number, next?: ListNode | null) {
301
+ * this.val = (val===undefined ? 0 : val)
302
+ * this.next = (next===undefined ? null : next)
303
+ * }
304
+ * }
305
+ */
306
+
307
+ function swapPairs(head : ListNode | null ): ListNode | null {
308
+ const dummy = new ListNode (0 , head );
309
+ let [pre, cur] = [dummy , head ];
310
+ while (cur && cur .next ) {
311
+ const t = cur .next ;
312
+ cur .next = t .next ;
313
+ t .next = cur ;
314
+ pre .next = t ;
315
+ [pre , cur ] = [cur , cur .next ];
316
+ }
317
+ return dummy .next ;
220
318
}
221
319
```
222
320
321
+ ### ** JavaScript**
322
+
323
+ ``` js
324
+ /**
325
+ * Definition for singly-linked list.
326
+ * function ListNode(val, next) {
327
+ * this.val = (val===undefined ? 0 : val)
328
+ * this.next = (next===undefined ? null : next)
329
+ * }
330
+ */
331
+ /**
332
+ * @param {ListNode} head
333
+ * @return {ListNode}
334
+ */
335
+ var swapPairs = function (head ) {
336
+ if (! head || ! head .next ) {
337
+ return head;
338
+ }
339
+ const t = swapPairs (head .next .next );
340
+ const p = head .next ;
341
+ p .next = head;
342
+ head .next = t;
343
+ return p;
344
+ };
345
+ ```
346
+
347
+ ``` js
348
+ /**
349
+ * Definition for singly-linked list.
350
+ * function ListNode(val, next) {
351
+ * this.val = (val===undefined ? 0 : val)
352
+ * this.next = (next===undefined ? null : next)
353
+ * }
354
+ */
355
+ /**
356
+ * @param {ListNode} head
357
+ * @return {ListNode}
358
+ */
359
+ var swapPairs = function (head ) {
360
+ const dummy = new ListNode (0 , head);
361
+ let [pre, cur] = [dummy, head];
362
+ while (cur && cur .next ) {
363
+ const t = cur .next ;
364
+ cur .next = t .next ;
365
+ t .next = cur;
366
+ pre .next = t;
367
+ [pre, cur] = [cur, cur .next ];
368
+ }
369
+ return dummy .next ;
370
+ };
371
+ ```
372
+
223
373
### ** Ruby**
224
374
225
375
``` rb
@@ -249,34 +399,6 @@ def swap_pairs(head)
249
399
end
250
400
```
251
401
252
- ### ** TypeScript**
253
-
254
- ``` ts
255
- /**
256
- * Definition for singly-linked list.
257
- * class ListNode {
258
- * val: number
259
- * next: ListNode | null
260
- * constructor(val?: number, next?: ListNode | null) {
261
- * this.val = (val===undefined ? 0 : val)
262
- * this.next = (next===undefined ? null : next)
263
- * }
264
- * }
265
- */
266
-
267
- function swapPairs(head : ListNode | null ): ListNode | null {
268
- const dummy = new ListNode (0 , head );
269
- let cur = dummy ;
270
- while (cur .next != null && cur .next .next != null ) {
271
- const a = cur .next ;
272
- const b = cur .next .next ;
273
- [a .next , b .next , cur .next ] = [b .next , a , b ];
274
- cur = cur .next .next ;
275
- }
276
- return dummy .next ;
277
- }
278
- ```
279
-
280
402
### ** Rust**
281
403
282
404
``` rust
0 commit comments