Skip to content

Commit 9de213a

Browse files
committed
feat: add solutions to lc problem: No.0206
No.0206.Reverse Linked List
1 parent 1c2fa27 commit 9de213a

File tree

4 files changed

+345
-0
lines changed

4 files changed

+345
-0
lines changed

solution/0200-0299/0206.Reverse Linked List/README.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,149 @@ public class Solution {
206206
}
207207
```
208208

209+
### **TypeScript**
210+
211+
循环:
212+
213+
```ts
214+
/**
215+
* Definition for singly-linked list.
216+
* class ListNode {
217+
* val: number
218+
* next: ListNode | null
219+
* constructor(val?: number, next?: ListNode | null) {
220+
* this.val = (val===undefined ? 0 : val)
221+
* this.next = (next===undefined ? null : next)
222+
* }
223+
* }
224+
*/
225+
226+
function reverseList(head: ListNode | null): ListNode | null {
227+
if (head == null) {
228+
return head;
229+
}
230+
let pre = null;
231+
let cur = head;
232+
while (cur != null) {
233+
const next = cur.next;
234+
cur.next = pre;
235+
[pre, cur] = [cur, next];
236+
}
237+
return pre;
238+
}
239+
```
240+
241+
递归:
242+
243+
```ts
244+
/**
245+
* Definition for singly-linked list.
246+
* class ListNode {
247+
* val: number
248+
* next: ListNode | null
249+
* constructor(val?: number, next?: ListNode | null) {
250+
* this.val = (val===undefined ? 0 : val)
251+
* this.next = (next===undefined ? null : next)
252+
* }
253+
* }
254+
*/
255+
const rev = (pre: ListNode | null, cur: ListNode | null): ListNode | null => {
256+
if (cur == null) {
257+
return pre;
258+
}
259+
const next = cur.next;
260+
cur.next = pre;
261+
return rev(cur, next);
262+
};
263+
264+
function reverseList(head: ListNode | null): ListNode | null {
265+
if (head == null) {
266+
return head;
267+
}
268+
const next = head.next;
269+
head.next = null;
270+
return rev(head, next);
271+
}
272+
```
273+
274+
### **Rust**
275+
276+
循环:
277+
278+
```rust
279+
// Definition for singly-linked list.
280+
// #[derive(PartialEq, Eq, Clone, Debug)]
281+
// pub struct ListNode {
282+
// pub val: i32,
283+
// pub next: Option<Box<ListNode>>
284+
// }
285+
//
286+
// impl ListNode {
287+
// #[inline]
288+
// fn new(val: i32) -> Self {
289+
// ListNode {
290+
// next: None,
291+
// val
292+
// }
293+
// }
294+
// }
295+
impl Solution {
296+
pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
297+
match head {
298+
None => None,
299+
Some(mut head) => {
300+
let mut cur = head.next.take();
301+
let mut pre = Some(head);
302+
while let Some(mut node) = cur {
303+
let next = node.next.take();
304+
node.next = pre;
305+
pre = Some(node);
306+
cur = next;
307+
}
308+
pre
309+
}
310+
}
311+
}
312+
}
313+
```
314+
315+
递归:
316+
317+
```rust
318+
// Definition for singly-linked list.
319+
// #[derive(PartialEq, Eq, Clone, Debug)]
320+
// pub struct ListNode {
321+
// pub val: i32,
322+
// pub next: Option<Box<ListNode>>
323+
// }
324+
//
325+
// impl ListNode {
326+
// #[inline]
327+
// fn new(val: i32) -> Self {
328+
// ListNode {
329+
// next: None,
330+
// val
331+
// }
332+
// }
333+
// }
334+
impl Solution {
335+
fn rev (pre: Option<Box<ListNode>>, cur: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
336+
match cur {
337+
None => pre,
338+
Some(mut node) => {
339+
let next = node.next;
340+
node.next = pre;
341+
Self::rev(Some(node), next)
342+
},
343+
}
344+
}
345+
346+
pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
347+
Self::rev(None, head)
348+
}
349+
}
350+
```
351+
209352
### **...**
210353

211354
```

solution/0200-0299/0206.Reverse Linked List/README_EN.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,149 @@ public class Solution {
223223
}
224224
```
225225

226+
### **TypeScript**
227+
228+
Loop:
229+
230+
```ts
231+
/**
232+
* Definition for singly-linked list.
233+
* class ListNode {
234+
* val: number
235+
* next: ListNode | null
236+
* constructor(val?: number, next?: ListNode | null) {
237+
* this.val = (val===undefined ? 0 : val)
238+
* this.next = (next===undefined ? null : next)
239+
* }
240+
* }
241+
*/
242+
243+
function reverseList(head: ListNode | null): ListNode | null {
244+
if (head == null) {
245+
return head;
246+
}
247+
let pre = null;
248+
let cur = head;
249+
while (cur != null) {
250+
const next = cur.next;
251+
cur.next = pre;
252+
[pre, cur] = [cur, next];
253+
}
254+
return pre;
255+
}
256+
```
257+
258+
Recursion:
259+
260+
```ts
261+
/**
262+
* Definition for singly-linked list.
263+
* class ListNode {
264+
* val: number
265+
* next: ListNode | null
266+
* constructor(val?: number, next?: ListNode | null) {
267+
* this.val = (val===undefined ? 0 : val)
268+
* this.next = (next===undefined ? null : next)
269+
* }
270+
* }
271+
*/
272+
const rev = (pre: ListNode | null, cur: ListNode | null): ListNode | null => {
273+
if (cur == null) {
274+
return pre;
275+
}
276+
const next = cur.next;
277+
cur.next = pre;
278+
return rev(cur, next);
279+
};
280+
281+
function reverseList(head: ListNode | null): ListNode | null {
282+
if (head == null) {
283+
return head;
284+
}
285+
const next = head.next;
286+
head.next = null;
287+
return rev(head, next);
288+
}
289+
```
290+
291+
### **Rust**
292+
293+
Loop:
294+
295+
```rust
296+
// Definition for singly-linked list.
297+
// #[derive(PartialEq, Eq, Clone, Debug)]
298+
// pub struct ListNode {
299+
// pub val: i32,
300+
// pub next: Option<Box<ListNode>>
301+
// }
302+
//
303+
// impl ListNode {
304+
// #[inline]
305+
// fn new(val: i32) -> Self {
306+
// ListNode {
307+
// next: None,
308+
// val
309+
// }
310+
// }
311+
// }
312+
impl Solution {
313+
pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
314+
match head {
315+
None => None,
316+
Some(mut head) => {
317+
let mut cur = head.next.take();
318+
let mut pre = Some(head);
319+
while let Some(mut node) = cur {
320+
let next = node.next.take();
321+
node.next = pre;
322+
pre = Some(node);
323+
cur = next;
324+
}
325+
pre
326+
}
327+
}
328+
}
329+
}
330+
```
331+
332+
Recursion:
333+
334+
```rust
335+
// Definition for singly-linked list.
336+
// #[derive(PartialEq, Eq, Clone, Debug)]
337+
// pub struct ListNode {
338+
// pub val: i32,
339+
// pub next: Option<Box<ListNode>>
340+
// }
341+
//
342+
// impl ListNode {
343+
// #[inline]
344+
// fn new(val: i32) -> Self {
345+
// ListNode {
346+
// next: None,
347+
// val
348+
// }
349+
// }
350+
// }
351+
impl Solution {
352+
fn rev (pre: Option<Box<ListNode>>, cur: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
353+
match cur {
354+
None => pre,
355+
Some(mut node) => {
356+
let next = node.next;
357+
node.next = pre;
358+
Self::rev(Some(node), next)
359+
},
360+
}
361+
}
362+
363+
pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
364+
Self::rev(None, head)
365+
}
366+
}
367+
```
368+
226369
### **...**
227370

228371
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
19+
match head {
20+
None => None,
21+
Some(mut head) => {
22+
let mut cur = head.next.take();
23+
let mut pre = Some(head);
24+
while let Some(mut node) = cur {
25+
let next = node.next.take();
26+
node.next = pre;
27+
pre = Some(node);
28+
cur = next;
29+
}
30+
pre
31+
}
32+
}
33+
}
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 reverseList(head: ListNode | null): ListNode | null {
14+
if (head == null) {
15+
return head;
16+
}
17+
let pre = null;
18+
let cur = head;
19+
while (cur != null) {
20+
const next = cur.next;
21+
cur.next = pre;
22+
[pre, cur] = [cur, next];
23+
}
24+
return pre;
25+
}

0 commit comments

Comments
 (0)