Skip to content

Commit 38514d5

Browse files
committed
feat: update solutions to lc problem: No.0024. Swap Nodes in Pairs
1 parent 0a79c65 commit 38514d5

File tree

8 files changed

+238
-64
lines changed

8 files changed

+238
-64
lines changed

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

+92-10
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@
4646

4747
<p><strong>进阶:</strong>你能在不修改链表节点值的情况下解决这个问题吗?(也就是说,仅修改节点本身。)</p>
4848

49-
5049
## 解法
5150

5251
<!-- 这里可写通用的实现逻辑 -->
5352

53+
设置虚拟头节点 dummy,pre 指针初始指向 dummy,遍历链表,每次交换 pre 后面的两个节点即可。
54+
5455
<!-- tabs:start -->
5556

5657
### **Python3**
@@ -72,8 +73,7 @@ class Solution:
7273
cur.next = t.next
7374
t.next = cur
7475
pre.next = t
75-
pre = cur
76-
cur = pre.next
76+
pre, cur = cur, cur.next
7777
return dummy.next
7878
```
7979

@@ -102,14 +102,43 @@ class Solution {
102102
t.next = cur;
103103
pre.next = t;
104104
pre = cur;
105-
cur = pre.next;
106-
105+
cur = cur.next;
107106
}
108107
return dummy.next;
109108
}
110109
}
111110
```
112111

112+
### **JavaScript**
113+
114+
```js
115+
/**
116+
* Definition for singly-linked list.
117+
* function ListNode(val, next) {
118+
* this.val = (val===undefined ? 0 : val)
119+
* this.next = (next===undefined ? null : next)
120+
* }
121+
*/
122+
/**
123+
* @param {ListNode} head
124+
* @return {ListNode}
125+
*/
126+
var swapPairs = function(head) {
127+
const dummy = new ListNode(0, head);
128+
let pre = dummy;
129+
let cur = head;
130+
while (cur && cur.next) {
131+
const t = cur.next;
132+
cur.next = t.next;
133+
t.next = cur;
134+
pre.next = t;
135+
pre = cur;
136+
cur = cur.next;
137+
}
138+
return dummy.next;
139+
};
140+
```
141+
113142
### **C++**
114143

115144
```cpp
@@ -126,22 +155,75 @@ class Solution {
126155
class Solution {
127156
public:
128157
ListNode* swapPairs(ListNode* head) {
129-
ListNode* dummy = new ListNode(0, head);
130-
ListNode* pre = dummy;
131-
ListNode* cur = head;
158+
ListNode *dummy = new ListNode(0, head);
159+
ListNode *pre = dummy, *cur = head;
132160
while (cur != nullptr && cur->next != nullptr) {
133-
ListNode* t = cur->next;
161+
ListNode *t = cur->next;
134162
cur->next = t->next;
135163
t->next = cur;
136164
pre->next = t;
137165
pre = cur;
138-
cur = pre->next;
166+
cur = cur->next;
139167
}
140168
return dummy->next;
141169
}
142170
};
143171
```
144172
173+
### **Go**
174+
175+
```go
176+
/**
177+
* Definition for singly-linked list.
178+
* type ListNode struct {
179+
* Val int
180+
* Next *ListNode
181+
* }
182+
*/
183+
func swapPairs(head *ListNode) *ListNode {
184+
dummy := &ListNode{0, head}
185+
pre, cur := dummy, head
186+
for cur != nil && cur.Next != nil {
187+
t := cur.Next
188+
cur.Next = t.Next
189+
t.Next = cur
190+
pre.Next = t
191+
pre = cur
192+
cur = cur.Next
193+
}
194+
return dummy.Next
195+
}
196+
```
197+
198+
### **Ruby**
199+
200+
```rb
201+
# Definition for singly-linked list.
202+
# class ListNode
203+
# attr_accessor :val, :next
204+
# def initialize(val = 0, _next = nil)
205+
# @val = val
206+
# @next = _next
207+
# end
208+
# end
209+
# @param {ListNode} head
210+
# @return {ListNode}
211+
def swap_pairs(head)
212+
dummy = ListNode.new(0, head)
213+
pre = dummy
214+
cur = head
215+
while !cur.nil? && !cur.next.nil?
216+
t = cur.next
217+
cur.next = t.next
218+
t.next = cur
219+
pre.next = t
220+
pre = cur
221+
cur = cur.next
222+
end
223+
dummy.next
224+
end
225+
```
226+
145227
### **...**
146228

147229
```

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

+90-9
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ class Solution:
6060
cur.next = t.next
6161
t.next = cur
6262
pre.next = t
63-
pre = cur
64-
cur = pre.next
63+
pre, cur = cur, cur.next
6564
return dummy.next
6665
```
6766

@@ -88,14 +87,43 @@ class Solution {
8887
t.next = cur;
8988
pre.next = t;
9089
pre = cur;
91-
cur = pre.next;
92-
90+
cur = cur.next;
9391
}
9492
return dummy.next;
9593
}
9694
}
9795
```
9896

97+
### **JavaScript**
98+
99+
```js
100+
/**
101+
* Definition for singly-linked list.
102+
* function ListNode(val, next) {
103+
* this.val = (val===undefined ? 0 : val)
104+
* this.next = (next===undefined ? null : next)
105+
* }
106+
*/
107+
/**
108+
* @param {ListNode} head
109+
* @return {ListNode}
110+
*/
111+
var swapPairs = function(head) {
112+
const dummy = new ListNode(0, head);
113+
let pre = dummy;
114+
let cur = head;
115+
while (cur && cur.next) {
116+
const t = cur.next;
117+
cur.next = t.next;
118+
t.next = cur;
119+
pre.next = t;
120+
pre = cur;
121+
cur = cur.next;
122+
}
123+
return dummy.next;
124+
};
125+
```
126+
99127
### **C++**
100128

101129
```cpp
@@ -112,22 +140,75 @@ class Solution {
112140
class Solution {
113141
public:
114142
ListNode* swapPairs(ListNode* head) {
115-
ListNode* dummy = new ListNode(0, head);
116-
ListNode* pre = dummy;
117-
ListNode* cur = head;
143+
ListNode *dummy = new ListNode(0, head);
144+
ListNode *pre = dummy, *cur = head;
118145
while (cur != nullptr && cur->next != nullptr) {
119-
ListNode* t = cur->next;
146+
ListNode *t = cur->next;
120147
cur->next = t->next;
121148
t->next = cur;
122149
pre->next = t;
123150
pre = cur;
124-
cur = pre->next;
151+
cur = cur->next;
125152
}
126153
return dummy->next;
127154
}
128155
};
129156
```
130157
158+
### **Go**
159+
160+
```go
161+
/**
162+
* Definition for singly-linked list.
163+
* type ListNode struct {
164+
* Val int
165+
* Next *ListNode
166+
* }
167+
*/
168+
func swapPairs(head *ListNode) *ListNode {
169+
dummy := &ListNode{0, head}
170+
pre, cur := dummy, head
171+
for cur != nil && cur.Next != nil {
172+
t := cur.Next
173+
cur.Next = t.Next
174+
t.Next = cur
175+
pre.Next = t
176+
pre = cur
177+
cur = cur.Next
178+
}
179+
return dummy.Next
180+
}
181+
```
182+
183+
### **Ruby**
184+
185+
```rb
186+
# Definition for singly-linked list.
187+
# class ListNode
188+
# attr_accessor :val, :next
189+
# def initialize(val = 0, _next = nil)
190+
# @val = val
191+
# @next = _next
192+
# end
193+
# end
194+
# @param {ListNode} head
195+
# @return {ListNode}
196+
def swap_pairs(head)
197+
dummy = ListNode.new(0, head)
198+
pre = dummy
199+
cur = head
200+
while !cur.nil? && !cur.next.nil?
201+
t = cur.next
202+
cur.next = t.next
203+
t.next = cur
204+
pre.next = t
205+
pre = cur
206+
cur = cur.next
207+
end
208+
dummy.next
209+
end
210+
```
211+
131212
### **...**
132213

133214
```

solution/0000-0099/0024.Swap Nodes in Pairs/Solution.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@
1111
class Solution {
1212
public:
1313
ListNode* swapPairs(ListNode* head) {
14-
ListNode* dummy = new ListNode(0, head);
15-
ListNode* pre = dummy;
16-
ListNode* cur = head;
14+
ListNode *dummy = new ListNode(0, head);
15+
ListNode *pre = dummy, *cur = head;
1716
while (cur != nullptr && cur->next != nullptr) {
18-
ListNode* t = cur->next;
17+
ListNode *t = cur->next;
1918
cur->next = t->next;
2019
t->next = cur;
2120
pre->next = t;
2221
pre = cur;
23-
cur = pre->next;
22+
cur = cur->next;
2423
}
2524
return dummy->next;
2625
}

solution/0000-0099/0024.Swap Nodes in Pairs/Solution.go

+11-23
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,15 @@
66
* }
77
*/
88
func swapPairs(head *ListNode) *ListNode {
9-
if head == nil || head.Next == nil {
10-
return head
11-
}
12-
first := head
13-
second := head.Next
14-
r := second
15-
var p *ListNode = nil
16-
for first != nil && second != nil {
17-
t := second.Next
18-
second.Next = first
19-
first.Next = t
20-
if p != nil {
21-
p.Next = second
22-
}
23-
p = first
24-
if t != nil {
25-
second = t.Next
26-
} else {
27-
second = nil
28-
}
29-
first = t
30-
}
31-
return r
9+
dummy := &ListNode{0, head}
10+
pre, cur := dummy, head
11+
for cur != nil && cur.Next != nil {
12+
t := cur.Next
13+
cur.Next = t.Next
14+
t.Next = cur
15+
pre.Next = t
16+
pre = cur
17+
cur = cur.Next
18+
}
19+
return dummy.Next
3220
}

solution/0000-0099/0024.Swap Nodes in Pairs/Solution.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ public ListNode swapPairs(ListNode head) {
1818
t.next = cur;
1919
pre.next = t;
2020
pre = cur;
21-
cur = pre.next;
22-
21+
cur = cur.next;
2322
}
2423
return dummy.next;
2524
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {ListNode}
11+
*/
12+
var swapPairs = function(head) {
13+
const dummy = new ListNode(0, head);
14+
let pre = dummy;
15+
let cur = head;
16+
while (cur && cur.next) {
17+
const t = cur.next;
18+
cur.next = t.next;
19+
t.next = cur;
20+
pre.next = t;
21+
pre = cur;
22+
cur = cur.next;
23+
}
24+
return dummy.next;
25+
};

solution/0000-0099/0024.Swap Nodes in Pairs/Solution.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@ def swapPairs(self, head: ListNode) -> ListNode:
1212
cur.next = t.next
1313
t.next = cur
1414
pre.next = t
15-
pre = cur
16-
cur = pre.next
15+
pre, cur = cur, cur.next
1716
return dummy.next

0 commit comments

Comments
 (0)