Skip to content

Commit 4c3abd6

Browse files
authoredJan 11, 2022
feat: add solutions to lcof problem: No.24 (doocs#665)
1 parent 324d148 commit 4c3abd6

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed
 

‎lcof/面试题24. 反转链表/README.md

+63-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class Solution {
8484
* @param {ListNode} head
8585
* @return {ListNode}
8686
*/
87-
var reverseList = function (head) {
87+
var reverseList = function(head) {
8888
let node = head;
8989
let pre = null;
9090
while (node) {
@@ -146,6 +146,68 @@ public:
146146
};
147147
```
148148

149+
### **TypeScript**
150+
151+
```ts
152+
/**
153+
* Definition for singly-linked list.
154+
* class ListNode {
155+
* val: number
156+
* next: ListNode | null
157+
* constructor(val?: number, next?: ListNode | null) {
158+
* this.val = (val===undefined ? 0 : val)
159+
* this.next = (next===undefined ? null : next)
160+
* }
161+
* }
162+
*/
163+
164+
function reverseList(head: ListNode | null): ListNode | null {
165+
let cur = head;
166+
let pre = null;
167+
while (cur != null) {
168+
const temp = cur.next;
169+
cur.next = pre;
170+
pre = cur;
171+
cur = temp;
172+
}
173+
return pre;
174+
}
175+
```
176+
177+
### **Rust**
178+
179+
```rust
180+
// Definition for singly-linked list.
181+
// #[derive(PartialEq, Eq, Clone, Debug)]
182+
// pub struct ListNode {
183+
// pub val: i32,
184+
// pub next: Option<Box<ListNode>>
185+
// }
186+
//
187+
// impl ListNode {
188+
// #[inline]
189+
// fn new(val: i32) -> Self {
190+
// ListNode {
191+
// next: None,
192+
// val
193+
// }
194+
// }
195+
// }
196+
impl Solution {
197+
pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
198+
let mut pre = None;
199+
let mut cur = head;
200+
201+
while let Some(mut node) = cur {
202+
cur = node.next.take();
203+
node.next = pre.take();
204+
pre = Some(node);
205+
}
206+
pre
207+
}
208+
}
209+
```
210+
149211
### **...**
150212

151213
```
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
let mut pre = None;
20+
let mut cur = head;
21+
22+
while let Some(mut node) = cur {
23+
cur = node.next.take();
24+
node.next = pre.take();
25+
pre = Some(node);
26+
}
27+
pre
28+
}
29+
}
+23
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 reverseList(head: ListNode | null): ListNode | null {
14+
let cur = head;
15+
let pre = null;
16+
while (cur != null) {
17+
const temp = cur.next;
18+
cur.next = pre;
19+
pre = cur;
20+
cur = temp;
21+
}
22+
return pre;
23+
}

0 commit comments

Comments
 (0)