Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1efa4f8

Browse files
committed
feat: update solution to lc problem: No.0024
No.0024.Swap Nodes in Pairs
1 parent 1a8fc8b commit 1efa4f8

File tree

4 files changed

+71
-13
lines changed

4 files changed

+71
-13
lines changed

‎solution/0000-0099/0018.4Sum/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
**方法一:排序 + 双指针**
5050

51-
该题和 [0015.三数之和](../0015.3Sum/README.md) 相似,解法也相似。
51+
该题和 [0015.三数之和](https://leetcode.cn/problems/3sum/) 相似,解法也相似。
5252

5353
时间复杂度为 $O(n^3),ドル空间复杂度为 $O(\log n),ドル其中 $n$ 是数组的长度。
5454

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,16 @@
4444

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

47+
**方法一:迭代**
48+
4749
设置虚拟头节点 dummy,pre 指针初始指向 dummy,遍历链表,每次交换 pre 后面的两个节点即可。
4850

51+
时间复杂度为 $O(n),ドル空间复杂度为 $O(1),ドル其中 $n$ 是链表的长度。
52+
53+
**方法二:递归**
54+
55+
时间复杂度为 $O(n),ドル空间复杂度为 $O(n),ドル其中 $n$ 是链表的长度。
56+
4957
<!-- tabs:start -->
5058

5159
### **Python3**
@@ -166,6 +174,8 @@ public:
166174
167175
### **Go**
168176
177+
迭代:
178+
169179
```go
170180
/**
171181
* Definition for singly-linked list.
@@ -189,6 +199,27 @@ func swapPairs(head *ListNode) *ListNode {
189199
}
190200
```
191201

202+
递归:
203+
204+
```go
205+
/**
206+
* Definition for singly-linked list.
207+
* type ListNode struct {
208+
* Val int
209+
* Next *ListNode
210+
* }
211+
*/
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
220+
}
221+
```
222+
192223
### **Ruby**
193224

194225
```rb

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@
3838

3939
## Solutions
4040

41+
**Approach 1: Iteration**
42+
43+
Time complexity $O(n),ドル Space complexity $O(1)$.
44+
45+
**Approach 2: Recursion**
46+
47+
Time complexity $O(n),ドル Space complexity $O(n)$.
48+
4149
<!-- tabs:start -->
4250

4351
### **Python3**
@@ -154,6 +162,8 @@ public:
154162
155163
### **Go**
156164
165+
Iteration:
166+
157167
```go
158168
/**
159169
* Definition for singly-linked list.
@@ -177,6 +187,27 @@ func swapPairs(head *ListNode) *ListNode {
177187
}
178188
```
179189

190+
Recursion:
191+
192+
```go
193+
/**
194+
* Definition for singly-linked list.
195+
* type ListNode struct {
196+
* Val int
197+
* Next *ListNode
198+
* }
199+
*/
200+
func swapPairs(head *ListNode) *ListNode {
201+
if head == nil || head.Next == nil {
202+
return head
203+
}
204+
res := swapPairs(head.Next.Next)
205+
p := head.Next
206+
p.Next, head.Next = head, res
207+
return p
208+
}
209+
```
210+
180211
### **Ruby**
181212

182213
```rb

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@
55
* Next *ListNode
66
* }
77
*/
8-
func swapPairs(head *ListNode) *ListNode {
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
8+
func swapPairs(head *ListNode) *ListNode {
9+
if head == nil || head.Next == nil {
10+
return head
11+
}
12+
res := swapPairs(head.Next.Next)
13+
p := head.Next
14+
p.Next, head.Next = head, res
15+
return p
2016
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /