-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathlib.rs
35 lines (32 loc) · 819 Bytes
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Definition for singly-linked list.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>,
}
impl ListNode {
#[inline]
fn new(val: i32) -> Self {
ListNode { next: None, val }
}
}
struct Solution {}
fn delete_node(head: Option<Box<ListNode>>, val: i32) -> Option<Box<ListNode>> {
let mut head = Some(Box::new(ListNode { val: 1, next: head }));
let mut root = &mut head;
while let Some(node) = root {
let next_node = &mut node.next;
match next_node {
None => break,
Some(next_node) => {
if next_node.val == val {
// 当前节点的下一个节点等于目标节点
node.next = next_node.next.take();
break;
}
}
}
root = &mut node.next;
}
head.unwrap().next
}