Skip to content

Commit dfde5b9

Browse files
committed
feat: add solutions to lc problem: No.0024
No.0024.Swap Nodes in Pairs
1 parent de7edd5 commit dfde5b9

File tree

4 files changed

+188
-0
lines changed

4 files changed

+188
-0
lines changed

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,72 @@ def swap_pairs(head)
218218
end
219219
```
220220

221+
### **TypeScript**
222+
223+
```ts
224+
/**
225+
* Definition for singly-linked list.
226+
* class ListNode {
227+
* val: number
228+
* next: ListNode | null
229+
* constructor(val?: number, next?: ListNode | null) {
230+
* this.val = (val===undefined ? 0 : val)
231+
* this.next = (next===undefined ? null : next)
232+
* }
233+
* }
234+
*/
235+
236+
function swapPairs(head: ListNode | null): ListNode | null {
237+
const dummy = new ListNode(0, head);
238+
let cur = dummy;
239+
while (cur.next != null && cur.next.next != null) {
240+
const a = cur.next;
241+
const b = cur.next.next;
242+
[a.next, b.next, cur.next] = [b.next, a, b];
243+
cur = cur.next.next;
244+
}
245+
return dummy.next;
246+
}
247+
```
248+
249+
### **Rust**
250+
251+
```rust
252+
// Definition for singly-linked list.
253+
// #[derive(PartialEq, Eq, Clone, Debug)]
254+
// pub struct ListNode {
255+
// pub val: i32,
256+
// pub next: Option<Box<ListNode>>
257+
// }
258+
//
259+
// impl ListNode {
260+
// #[inline]
261+
// fn new(val: i32) -> Self {
262+
// ListNode {
263+
// next: None,
264+
// val
265+
// }
266+
// }
267+
// }
268+
impl Solution {
269+
pub fn swap_pairs(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
270+
let mut dummy = Some(Box::new(ListNode { val: 0, next: head }));
271+
let mut cur = dummy.as_mut().unwrap();
272+
while cur.next.is_some() && cur.next.as_ref().unwrap().next.is_some() {
273+
cur.next = {
274+
let mut b = cur.next.as_mut().unwrap().next.take();
275+
cur.next.as_mut().unwrap().next = b.as_mut().unwrap().next.take();
276+
let a = cur.next.take();
277+
b.as_mut().unwrap().next = a;
278+
b
279+
};
280+
cur = cur.next.as_mut().unwrap().next.as_mut().unwrap();
281+
}
282+
dummy.unwrap().next
283+
}
284+
}
285+
```
286+
221287
### **...**
222288

223289
```

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,72 @@ def swap_pairs(head)
206206
end
207207
```
208208

209+
### **TypeScript**
210+
211+
```ts
212+
/**
213+
* Definition for singly-linked list.
214+
* class ListNode {
215+
* val: number
216+
* next: ListNode | null
217+
* constructor(val?: number, next?: ListNode | null) {
218+
* this.val = (val===undefined ? 0 : val)
219+
* this.next = (next===undefined ? null : next)
220+
* }
221+
* }
222+
*/
223+
224+
function swapPairs(head: ListNode | null): ListNode | null {
225+
const dummy = new ListNode(0, head);
226+
let cur = dummy;
227+
while (cur.next != null && cur.next.next != null) {
228+
const a = cur.next;
229+
const b = cur.next.next;
230+
[a.next, b.next, cur.next] = [b.next, a, b];
231+
cur = cur.next.next;
232+
}
233+
return dummy.next;
234+
}
235+
```
236+
237+
### **Rust**
238+
239+
```rust
240+
// Definition for singly-linked list.
241+
// #[derive(PartialEq, Eq, Clone, Debug)]
242+
// pub struct ListNode {
243+
// pub val: i32,
244+
// pub next: Option<Box<ListNode>>
245+
// }
246+
//
247+
// impl ListNode {
248+
// #[inline]
249+
// fn new(val: i32) -> Self {
250+
// ListNode {
251+
// next: None,
252+
// val
253+
// }
254+
// }
255+
// }
256+
impl Solution {
257+
pub fn swap_pairs(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
258+
let mut dummy = Some(Box::new(ListNode { val: 0, next: head }));
259+
let mut cur = dummy.as_mut().unwrap();
260+
while cur.next.is_some() && cur.next.as_ref().unwrap().next.is_some() {
261+
cur.next = {
262+
let mut b = cur.next.as_mut().unwrap().next.take();
263+
cur.next.as_mut().unwrap().next = b.as_mut().unwrap().next.take();
264+
let a = cur.next.take();
265+
b.as_mut().unwrap().next = a;
266+
b
267+
};
268+
cur = cur.next.as_mut().unwrap().next.as_mut().unwrap();
269+
}
270+
dummy.unwrap().next
271+
}
272+
}
273+
```
274+
209275
### **...**
210276

211277
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Definition for singly-linked list.
2+
// #[derive(PartialEq, Eq, Clone, Debug)]
3+
// pub struct ListNode {
4+
// pub val: i32,
5+
// pub next: Option<Box<ListNode>>
6+
// }
7+
//
8+
// impl ListNode {
9+
// #[inline]
10+
// fn new(val: i32) -> Self {
11+
// ListNode {
12+
// next: None,
13+
// val
14+
// }
15+
// }
16+
// }
17+
impl Solution {
18+
pub fn swap_pairs(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
19+
let mut dummy = Some(Box::new(ListNode { val: 0, next: head }));
20+
let mut cur = dummy.as_mut().unwrap();
21+
while cur.next.is_some() && cur.next.as_ref().unwrap().next.is_some() {
22+
cur.next = {
23+
let mut b = cur.next.as_mut().unwrap().next.take();
24+
cur.next.as_mut().unwrap().next = b.as_mut().unwrap().next.take();
25+
let a = cur.next.take();
26+
b.as_mut().unwrap().next = a;
27+
b
28+
};
29+
cur = cur.next.as_mut().unwrap().next.as_mut().unwrap();
30+
}
31+
dummy.unwrap().next
32+
}
33+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 swapPairs(head: ListNode | null): ListNode | null {
14+
const dummy = new ListNode(0, head);
15+
let cur = dummy;
16+
while (cur.next != null && cur.next.next != null) {
17+
const a = cur.next;
18+
const b = cur.next.next;
19+
[a.next, b.next, cur.next] = [b.next, a, b];
20+
cur = cur.next.next;
21+
}
22+
return dummy.next;
23+
}

0 commit comments

Comments
 (0)