Skip to content

Commit 588b31c

Browse files
authoredOct 19, 2023
feat: add solutions to lc problem: No.1721 (#1846)
No.1721.Swapping Nodes in a Linked List
1 parent c64265d commit 588b31c

File tree

8 files changed

+228
-119
lines changed

8 files changed

+228
-119
lines changed
 

‎solution/1700-1799/1721.Swapping Nodes in a Linked List/README.md

+60-20
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@
6161

6262
<!-- 这里可写通用的实现逻辑 -->
6363

64+
**方法一:快慢指针**
65+
66+
我们可以先用快指针 $fast$ 找到链表的第 $k$ 个节点,用指针 $p$ 指向它。然后我们再用慢指针 $slow$ 从链表的头节点出发,快慢指针同时向后移动,当快指针到达链表的最后一个节点时,慢指针 $slow$ 恰好指向倒数第 $k$ 个节点,用指针 $q$ 指向它。此时,我们只需要交换 $p$ 和 $q$ 的值即可。
67+
68+
时间复杂度 $O(n)$,其中 $n$ 是链表的长度。空间复杂度 $O(1)$。
69+
6470
<!-- tabs:start -->
6571

6672
### **Python3**
@@ -74,14 +80,13 @@
7480
# self.val = val
7581
# self.next = next
7682
class Solution:
77-
def swapNodes(self, head: ListNode, k: int) -> ListNode:
78-
fast = head
83+
def swapNodes(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
84+
fast = slow = head
7985
for _ in range(k - 1):
8086
fast = fast.next
8187
p = fast
82-
slow = head
8388
while fast.next:
84-
slow, fast = slow.next, fast.next
89+
fast, slow = fast.next, slow.next
8590
q = slow
8691
p.val, q.val = q.val, p.val
8792
return head
@@ -111,8 +116,8 @@ class Solution {
111116
ListNode p = fast;
112117
ListNode slow = head;
113118
while (fast.next != null) {
114-
slow = slow.next;
115119
fast = fast.next;
120+
slow = slow.next;
116121
}
117122
ListNode q = slow;
118123
int t = p.val;
@@ -139,16 +144,18 @@ class Solution {
139144
class Solution {
140145
public:
141146
ListNode* swapNodes(ListNode* head, int k) {
142-
ListNode* p1 = head;
143-
for (int i = 1; i < k; i++)
144-
p1 = p1->next;
145-
ListNode *slow = head, *fast = p1->next;
146-
147-
while (fast) {
147+
ListNode* fast = head;
148+
while (--k) {
149+
fast = fast->next;
150+
}
151+
ListNode* slow = head;
152+
ListNode* p = fast;
153+
while (fast->next) {
148154
fast = fast->next;
149155
slow = slow->next;
150156
}
151-
swap(slow->val, p1->val);
157+
ListNode* q = slow;
158+
swap(p->val, q->val);
152159
return head;
153160
}
154161
};
@@ -166,14 +173,13 @@ public:
166173
*/
167174
func swapNodes(head *ListNode, k int) *ListNode {
168175
fast := head
169-
for k > 1 {
176+
for ; k > 1; k-- {
170177
fast = fast.Next
171-
k--
172178
}
173179
p := fast
174180
slow := head
175181
for fast.Next != nil {
176-
slow, fast = slow.Next, fast.Next
182+
fast, slow = fast.Next, slow.Next
177183
}
178184
q := slow
179185
p.Val, q.Val = q.Val, p.Val
@@ -197,22 +203,56 @@ func swapNodes(head *ListNode, k int) *ListNode {
197203
*/
198204

199205
function swapNodes(head: ListNode | null, k: number): ListNode | null {
200-
let fast = head;
206+
let [fast, slow] = [head, head];
201207
while (--k) {
202208
fast = fast.next;
203209
}
204-
let p = fast;
205-
let slow = head;
210+
const p = fast;
206211
while (fast.next) {
207-
slow = slow.next;
208212
fast = fast.next;
213+
slow = slow.next;
209214
}
210-
let q = slow;
215+
const q = slow;
211216
[p.val, q.val] = [q.val, p.val];
212217
return head;
213218
}
214219
```
215220

221+
### **C#**
222+
223+
```cs
224+
/**
225+
* Definition for singly-linked list.
226+
* public class ListNode {
227+
* public int val;
228+
* public ListNode next;
229+
* public ListNode(int val=0, ListNode next=null) {
230+
* this.val = val;
231+
* this.next = next;
232+
* }
233+
* }
234+
*/
235+
public class Solution {
236+
public ListNode SwapNodes(ListNode head, int k) {
237+
ListNode fast = head;
238+
while (--k > 0) {
239+
fast = fast.next;
240+
}
241+
ListNode p = fast;
242+
ListNode slow = head;
243+
while (fast.next != null) {
244+
fast = fast.next;
245+
slow = slow.next;
246+
}
247+
ListNode q = slow;
248+
int t = p.val;
249+
p.val = q.val;
250+
q.val = t;
251+
return head;
252+
}
253+
}
254+
```
255+
216256
### **...**
217257

218258
```

‎solution/1700-1799/1721.Swapping Nodes in a Linked List/README_EN.md

+60-20
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434

3535
## Solutions
3636

37+
**Solution 1: Two Pointers**
38+
39+
We can first use a fast pointer `fast` to find the $k$th node of the linked list, and use a pointer `p` to point to it. Then, we use a slow pointer `slow` to start from the head node of the linked list, and move both pointers forward at the same time. When the fast pointer reaches the last node of the linked list, the slow pointer `slow` points to the $k$th node from the end of the linked list, and we use a pointer `q` to point to it. At this point, we only need to swap the values of `p` and `q`.
40+
41+
The time complexity is $O(n)$, where $n$ is the length of the linked list. The space complexity is $O(1)$.
42+
3743
<!-- tabs:start -->
3844

3945
### **Python3**
@@ -45,14 +51,13 @@
4551
# self.val = val
4652
# self.next = next
4753
class Solution:
48-
def swapNodes(self, head: ListNode, k: int) -> ListNode:
49-
fast = head
54+
def swapNodes(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
55+
fast = slow = head
5056
for _ in range(k - 1):
5157
fast = fast.next
5258
p = fast
53-
slow = head
5459
while fast.next:
55-
slow, fast = slow.next, fast.next
60+
fast, slow = fast.next, slow.next
5661
q = slow
5762
p.val, q.val = q.val, p.val
5863
return head
@@ -80,8 +85,8 @@ class Solution {
8085
ListNode p = fast;
8186
ListNode slow = head;
8287
while (fast.next != null) {
83-
slow = slow.next;
8488
fast = fast.next;
89+
slow = slow.next;
8590
}
8691
ListNode q = slow;
8792
int t = p.val;
@@ -108,16 +113,18 @@ class Solution {
108113
class Solution {
109114
public:
110115
ListNode* swapNodes(ListNode* head, int k) {
111-
ListNode* p1 = head;
112-
for (int i = 1; i < k; i++)
113-
p1 = p1->next;
114-
ListNode *slow = head, *fast = p1->next;
115-
116-
while (fast) {
116+
ListNode* fast = head;
117+
while (--k) {
118+
fast = fast->next;
119+
}
120+
ListNode* slow = head;
121+
ListNode* p = fast;
122+
while (fast->next) {
117123
fast = fast->next;
118124
slow = slow->next;
119125
}
120-
swap(slow->val, p1->val);
126+
ListNode* q = slow;
127+
swap(p->val, q->val);
121128
return head;
122129
}
123130
};
@@ -135,14 +142,13 @@ public:
135142
*/
136143
func swapNodes(head *ListNode, k int) *ListNode {
137144
fast := head
138-
for k > 1 {
145+
for ; k > 1; k-- {
139146
fast = fast.Next
140-
k--
141147
}
142148
p := fast
143149
slow := head
144150
for fast.Next != nil {
145-
slow, fast = slow.Next, fast.Next
151+
fast, slow = fast.Next, slow.Next
146152
}
147153
q := slow
148154
p.Val, q.Val = q.Val, p.Val
@@ -166,22 +172,56 @@ func swapNodes(head *ListNode, k int) *ListNode {
166172
*/
167173

168174
function swapNodes(head: ListNode | null, k: number): ListNode | null {
169-
let fast = head;
175+
let [fast, slow] = [head, head];
170176
while (--k) {
171177
fast = fast.next;
172178
}
173-
let p = fast;
174-
let slow = head;
179+
const p = fast;
175180
while (fast.next) {
176-
slow = slow.next;
177181
fast = fast.next;
182+
slow = slow.next;
178183
}
179-
let q = slow;
184+
const q = slow;
180185
[p.val, q.val] = [q.val, p.val];
181186
return head;
182187
}
183188
```
184189

190+
### **C#**
191+
192+
```cs
193+
/**
194+
* Definition for singly-linked list.
195+
* public class ListNode {
196+
* public int val;
197+
* public ListNode next;
198+
* public ListNode(int val=0, ListNode next=null) {
199+
* this.val = val;
200+
* this.next = next;
201+
* }
202+
* }
203+
*/
204+
public class Solution {
205+
public ListNode SwapNodes(ListNode head, int k) {
206+
ListNode fast = head;
207+
while (--k > 0) {
208+
fast = fast.next;
209+
}
210+
ListNode p = fast;
211+
ListNode slow = head;
212+
while (fast.next != null) {
213+
fast = fast.next;
214+
slow = slow.next;
215+
}
216+
ListNode q = slow;
217+
int t = p.val;
218+
p.val = q.val;
219+
q.val = t;
220+
return head;
221+
}
222+
}
223+
```
224+
185225
### **...**
186226

187227
```
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
/**
2-
* Definition for singly-linked list.
3-
* struct ListNode {
4-
* int val;
5-
* ListNode *next;
6-
* ListNode() : val(0), next(nullptr) {}
7-
* ListNode(int x) : val(x), next(nullptr) {}
8-
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9-
* };
10-
*/
11-
class Solution {
12-
public:
13-
ListNode* swapNodes(ListNode* head, int k) {
14-
ListNode* p1 = head;
15-
for (int i = 1; i < k; i++)
16-
p1 = p1->next;
17-
ListNode *slow = head, *fast = p1->next;
18-
19-
while (fast) {
20-
fast = fast->next;
21-
slow = slow->next;
22-
}
23-
swap(slow->val, p1->val);
24-
return head;
25-
}
26-
};
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* swapNodes(ListNode* head, int k) {
14+
ListNode* fast = head;
15+
while (--k) {
16+
fast = fast->next;
17+
}
18+
ListNode* slow = head;
19+
ListNode* p = fast;
20+
while (fast->next) {
21+
fast = fast->next;
22+
slow = slow->next;
23+
}
24+
ListNode* q = slow;
25+
swap(p->val, q->val);
26+
return head;
27+
}
28+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public int val;
5+
* public ListNode next;
6+
* public ListNode(int val=0, ListNode next=null) {
7+
* this.val = val;
8+
* this.next = next;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
public ListNode SwapNodes(ListNode head, int k) {
14+
ListNode fast = head;
15+
while (--k > 0) {
16+
fast = fast.next;
17+
}
18+
ListNode p = fast;
19+
ListNode slow = head;
20+
while (fast.next != null) {
21+
fast = fast.next;
22+
slow = slow.next;
23+
}
24+
ListNode q = slow;
25+
int t = p.val;
26+
p.val = q.val;
27+
q.val = t;
28+
return head;
29+
}
30+
}

‎solution/1700-1799/1721.Swapping Nodes in a Linked List/Solution.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
*/
88
func swapNodes(head *ListNode, k int) *ListNode {
99
fast := head
10-
for k > 1 {
10+
for ; k > 1; k-- {
1111
fast = fast.Next
12-
k--
1312
}
1413
p := fast
1514
slow := head
1615
for fast.Next != nil {
17-
slow, fast = slow.Next, fast.Next
16+
fast, slow = fast.Next, slow.Next
1817
}
1918
q := slow
2019
p.Val, q.Val = q.Val, p.Val
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
/**
2-
* Definition for singly-linked list.
3-
* public class ListNode {
4-
* int val;
5-
* ListNode next;
6-
* ListNode() {}
7-
* ListNode(int val) { this.val = val; }
8-
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9-
* }
10-
*/
11-
class Solution {
12-
public ListNode swapNodes(ListNode head, int k) {
13-
ListNode fast = head;
14-
while (--k > 0) {
15-
fast = fast.next;
16-
}
17-
ListNode p = fast;
18-
ListNode slow = head;
19-
while (fast.next != null) {
20-
slow = slow.next;
21-
fast = fast.next;
22-
}
23-
ListNode q = slow;
24-
int t = p.val;
25-
p.val = q.val;
26-
q.val = t;
27-
return head;
28-
}
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode swapNodes(ListNode head, int k) {
13+
ListNode fast = head;
14+
while (--k > 0) {
15+
fast = fast.next;
16+
}
17+
ListNode p = fast;
18+
ListNode slow = head;
19+
while (fast.next != null) {
20+
fast = fast.next;
21+
slow = slow.next;
22+
}
23+
ListNode q = slow;
24+
int t = p.val;
25+
p.val = q.val;
26+
q.val = t;
27+
return head;
28+
}
2929
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
# Definition for singly-linked list.
2-
# class ListNode:
3-
# def __init__(self, val=0, next=None):
4-
# self.val = val
5-
# self.next = next
6-
class Solution:
7-
def swapNodes(self, head: ListNode, k: int) -> ListNode:
8-
fast = head
9-
for _ in range(k - 1):
10-
fast = fast.next
11-
p = fast
12-
slow = head
13-
while fast.next:
14-
slow, fast = slow.next, fast.next
15-
q = slow
16-
p.val, q.val = q.val, p.val
17-
return head
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def swapNodes(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
8+
fast = slow = head
9+
for _ in range(k - 1):
10+
fast = fast.next
11+
p = fast
12+
while fast.next:
13+
fast, slow = fast.next, slow.next
14+
q = slow
15+
p.val, q.val = q.val, p.val
16+
return head

‎solution/1700-1799/1721.Swapping Nodes in a Linked List/Solution.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,16 @@
1111
*/
1212

1313
function swapNodes(head: ListNode | null, k: number): ListNode | null {
14-
let fast = head;
14+
let [fast, slow] = [head, head];
1515
while (--k) {
1616
fast = fast.next;
1717
}
18-
let p = fast;
19-
let slow = head;
18+
const p = fast;
2019
while (fast.next) {
21-
slow = slow.next;
2220
fast = fast.next;
21+
slow = slow.next;
2322
}
24-
let q = slow;
23+
const q = slow;
2524
[p.val, q.val] = [q.val, p.val];
2625
return head;
2726
}

0 commit comments

Comments
 (0)
Please sign in to comment.