Skip to content

Commit 616de74

Browse files
committed
feat: add solutions to lc problem: No.1721
No.1721.Swapping Nodes in a Linked List
1 parent a7cf06a commit 616de74

File tree

4 files changed

+179
-0
lines changed

4 files changed

+179
-0
lines changed

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,69 @@ public:
154154
};
155155
```
156156
157+
### **Go**
158+
159+
```go
160+
/**
161+
* Definition for singly-linked list.
162+
* type ListNode struct {
163+
* Val int
164+
* Next *ListNode
165+
* }
166+
*/
167+
func swapNodes(head *ListNode, k int) *ListNode {
168+
fast := head
169+
for k > 1 {
170+
fast = fast.Next
171+
k--
172+
}
173+
p := fast
174+
slow := head
175+
for fast.Next != nil {
176+
slow, fast = slow.Next, fast.Next
177+
}
178+
q := slow
179+
p.Val, q.Val = q.Val, p.Val
180+
return head
181+
}
182+
```
183+
184+
### **TypeScript**
185+
186+
```ts
187+
/**
188+
* Definition for singly-linked list.
189+
* class ListNode {
190+
* val: number
191+
* next: ListNode | null
192+
* constructor(val?: number, next?: ListNode | null) {
193+
* this.val = (val===undefined ? 0 : val)
194+
* this.next = (next===undefined ? null : next)
195+
* }
196+
* }
197+
*/
198+
199+
function swapNodes(head: ListNode | null, k: number): ListNode | null {
200+
let fast = head;
201+
while (--k) {
202+
fast = fast.next;
203+
}
204+
let p = fast;
205+
let slow = head;
206+
while (fast.next) {
207+
slow = slow.next;
208+
fast = fast.next;
209+
}
210+
let q = slow;
211+
[p.val, q.val] = [q.val, p.val];
212+
return head;
213+
}
214+
```
215+
216+
### **...**
217+
218+
```
219+
220+
```
221+
157222
<!-- tabs:end -->

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,69 @@ public:
123123
};
124124
```
125125
126+
### **Go**
127+
128+
```go
129+
/**
130+
* Definition for singly-linked list.
131+
* type ListNode struct {
132+
* Val int
133+
* Next *ListNode
134+
* }
135+
*/
136+
func swapNodes(head *ListNode, k int) *ListNode {
137+
fast := head
138+
for k > 1 {
139+
fast = fast.Next
140+
k--
141+
}
142+
p := fast
143+
slow := head
144+
for fast.Next != nil {
145+
slow, fast = slow.Next, fast.Next
146+
}
147+
q := slow
148+
p.Val, q.Val = q.Val, p.Val
149+
return head
150+
}
151+
```
152+
153+
### **TypeScript**
154+
155+
```ts
156+
/**
157+
* Definition for singly-linked list.
158+
* class ListNode {
159+
* val: number
160+
* next: ListNode | null
161+
* constructor(val?: number, next?: ListNode | null) {
162+
* this.val = (val===undefined ? 0 : val)
163+
* this.next = (next===undefined ? null : next)
164+
* }
165+
* }
166+
*/
167+
168+
function swapNodes(head: ListNode | null, k: number): ListNode | null {
169+
let fast = head;
170+
while (--k) {
171+
fast = fast.next;
172+
}
173+
let p = fast;
174+
let slow = head;
175+
while (fast.next) {
176+
slow = slow.next;
177+
fast = fast.next;
178+
}
179+
let q = slow;
180+
[p.val, q.val] = [q.val, p.val];
181+
return head;
182+
}
183+
```
184+
185+
### **...**
186+
187+
```
188+
189+
```
190+
126191
<!-- tabs:end -->
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
8+
func swapNodes(head *ListNode, k int) *ListNode {
9+
fast := head
10+
for k > 1 {
11+
fast = fast.Next
12+
k--
13+
}
14+
p := fast
15+
slow := head
16+
for fast.Next != nil {
17+
slow, fast = slow.Next, fast.Next
18+
}
19+
q := slow
20+
p.Val, q.Val = q.Val, p.Val
21+
return head
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
function swapNodes(head: ListNode | null, k: number): ListNode | null {
14+
let fast = head;
15+
while (--k) {
16+
fast = fast.next;
17+
}
18+
let p = fast;
19+
let slow = head;
20+
while (fast.next) {
21+
slow = slow.next;
22+
fast = fast.next;
23+
}
24+
let q = slow;
25+
[p.val, q.val] = [q.val, p.val];
26+
return head;
27+
}

0 commit comments

Comments
 (0)