forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.rs
39 lines (38 loc) · 1.02 KB
/
Solution.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
36
37
38
39
// 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
// }
// }
// }
use std::collections::HashSet;
impl Solution {
pub fn remove_duplicate_nodes(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
match head {
None => head,
Some(mut head) => {
let mut set = HashSet::new();
set.insert(head.val);
let mut pre = &mut head;
while let Some(cur) = &pre.next {
if set.contains(&cur.val) {
pre.next = pre.next.take().unwrap().next;
} else {
set.insert(cur.val);
pre = pre.next.as_mut().unwrap();
}
}
Some(head)
}
}
}
}