-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0203_remove_linked_list_elements.rs
More file actions
75 lines (69 loc) · 1.88 KB
/
s0203_remove_linked_list_elements.rs
File metadata and controls
75 lines (69 loc) · 1.88 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#![allow(unused)]
pub struct Solution {}
use crate::util::linked_list::{to_list, ListNode};
// 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
// }
// }
// }
impl Solution {
pub fn remove_elements(mut head: Option<Box<ListNode>>, val: i32) -> Option<Box<ListNode>> {
let mut p = head.as_mut();
while let Some(t) = p {
if t.val == val {
head = t.next.take();
p = head.as_mut();
} else if t.next.is_some() && val == t.next.as_ref().unwrap().val {
let tmp = t.next.as_mut().unwrap().next.take();
t.next = tmp;
p = Some(t);
}else {
p = t.next.as_mut();
}
}
head
}
pub fn remove_elements_a(mut head: Option<Box<ListNode>>, val: i32) -> Option<Box<ListNode>> {
let mut ptr = &mut head;
loop {
match ptr {
None => break,
Some(node) if node.val == val => {
*ptr = node.next.take();
}
Some(node) => {
ptr = &mut node.next;
}
}
}
head
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_203() {
assert_eq!(
Solution::remove_elements(to_list(vec![1, 2, 6, 3, 4, 5, 6]), 6),
to_list(vec![1, 2, 3, 4, 5])
);
assert_eq!(Solution::remove_elements(to_list(vec![1]), 1), None);
assert_eq!(
Solution::remove_elements_a(to_list(vec![1, 2, 6, 3, 4, 5, 6]), 6),
to_list(vec![1, 2, 3, 4, 5])
);
assert_eq!(Solution::remove_elements_a(to_list(vec![1]), 1), None); }
}