Skip to content

Commit 8524b9a

Browse files
committed
feat: add rust solution to lcof problem: No.18
面试题18. 删除链表的节点
1 parent 122393e commit 8524b9a

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

lcof/面试题18. 删除链表的节点/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,40 @@ public:
169169
};
170170
```
171171

172+
### **Rust**
173+
174+
```rust
175+
// Definition for singly-linked list.
176+
// #[derive(PartialEq, Eq, Clone, Debug)]
177+
// pub struct ListNode {
178+
// pub val: i32,
179+
// pub next: Option<Box<ListNode>>
180+
// }
181+
//
182+
// impl ListNode {
183+
// #[inline]
184+
// fn new(val: i32) -> Self {
185+
// ListNode {
186+
// next: None,
187+
// val
188+
// }
189+
// }
190+
// }
191+
impl Solution {
192+
pub fn delete_node(mut head: Option<Box<ListNode>>, val: i32) -> Option<Box<ListNode>> {
193+
let mut cur = &mut head;
194+
while let Some(node) = cur {
195+
if node.val == val {
196+
*cur = node.next.take();
197+
break;
198+
}
199+
cur = &mut cur.as_mut().unwrap().next;
200+
}
201+
head
202+
}
203+
}
204+
```
205+
172206
### **...**
173207

174208
```
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 delete_node(mut head: Option<Box<ListNode>>, val: i32) -> Option<Box<ListNode>> {
19+
let mut cur = &mut head;
20+
while let Some(node) = cur {
21+
if node.val == val {
22+
*cur = node.next.take();
23+
break;
24+
}
25+
cur = &mut cur.as_mut().unwrap().next;
26+
}
27+
head
28+
}
29+
}

0 commit comments

Comments
 (0)