Skip to content

Commit 6853bba

Browse files
authored
feat: add solutions to lc problem: No.0024 (doocs#1406)
No.0024.Swap Nodes in Pairs
1 parent da9edfc commit 6853bba

File tree

8 files changed

+478
-228
lines changed

8 files changed

+478
-228
lines changed

solution/0000-0099/0024.Swap Nodes in Pairs/README.md

+200-78
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,23 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47-
**方法一:迭代**
47+
**方法一:递归**
4848

49-
设置虚拟头节点 dummy,pre 指针初始指向 dummy,遍历链表,每次交换 pre 后面的两个节点即可
49+
我们可以通过递归的方式实现两两交换链表中的节点
5050

51-
时间复杂度为 $O(n)$,空间复杂度为 $O(1)$,其中 $n$ 是链表的长度
51+
递归的终止条件是链表中没有节点,或者链表中只有一个节点,此时无法进行交换,直接返回该节点
5252

53-
**方法二:递归**
53+
否则,我们递归交换链表 $head.next.next$,记交换后的头节点为 $t$,然后我们记 $head$ 的下一个节点为 $p$,然后令 $p$ 指向 $head$,而 $head$ 指向 $t$,最后返回 $p$。
5454

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$ 是链表的长度。
5664

5765
<!-- tabs:start -->
5866

@@ -67,7 +75,24 @@
6775
# self.val = val
6876
# self.next = next
6977
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]:
7196
dummy = ListNode(next=head)
7297
pre, cur = dummy, head
7398
while cur and cur.next:
@@ -83,6 +108,31 @@ class Solution:
83108

84109
<!-- 这里可写当前语言的特殊实现逻辑 -->
85110

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+
86136
```java
87137
/**
88138
* Definition for singly-linked list.
@@ -97,7 +147,8 @@ class Solution:
97147
class Solution {
98148
public ListNode swapPairs(ListNode head) {
99149
ListNode dummy = new ListNode(0, head);
100-
ListNode pre = dummy, cur = head;
150+
ListNode pre = dummy;
151+
ListNode cur = head;
101152
while (cur != null && cur.next != null) {
102153
ListNode t = cur.next;
103154
cur.next = t.next;
@@ -111,38 +162,34 @@ class Solution {
111162
}
112163
```
113164

114-
### **JavaScript**
165+
### **C++**
115166

116-
```js
167+
```cpp
117168
/**
118169
* 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+
* };
127177
*/
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;
139189
}
140-
return dummy.next;
141190
};
142191
```
143192
144-
### **C++**
145-
146193
```cpp
147194
/**
148195
* Definition for singly-linked list.
@@ -158,8 +205,9 @@ class Solution {
158205
public:
159206
ListNode* swapPairs(ListNode* head) {
160207
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) {
163211
ListNode* t = cur->next;
164212
cur->next = t->next;
165213
t->next = cur;
@@ -174,7 +222,25 @@ public:
174222

175223
### **Go**
176224

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+
```
178244

179245
```go
180246
/**
@@ -185,41 +251,125 @@ public:
185251
* }
186252
*/
187253
func swapPairs(head *ListNode) *ListNode {
188-
dummy := &ListNode{0, head}
254+
dummy := &ListNode{Next: head}
189255
pre, cur := dummy, head
190256
for cur != nil && cur.Next != nil {
191257
t := cur.Next
192258
cur.Next = t.Next
193259
t.Next = cur
194260
pre.Next = t
195-
pre = cur
196-
cur = cur.Next
261+
pre, cur = cur, cur.Next
197262
}
198263
return dummy.Next
199264
}
200265
```
201266

202-
递归:
267+
### **TypeScript**
203268

204-
```go
269+
```ts
205270
/**
206271
* 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+
* }
210279
* }
211280
*/
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;
220318
}
221319
```
222320

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+
223373
### **Ruby**
224374

225375
```rb
@@ -249,34 +399,6 @@ def swap_pairs(head)
249399
end
250400
```
251401

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-
280402
### **Rust**
281403

282404
```rust

0 commit comments

Comments
 (0)