-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathremove-duplicate-node-lcci.rs
41 lines (34 loc) · 1.04 KB
/
remove-duplicate-node-lcci.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
40
41
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {}
struct Solution;
// 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_duplicate_nodes(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut set: std::collections::HashSet<i32> = Default::default();
let mut tmp = Some(Box::new(ListNode { next: None, val: 0 }));
let mut current = &mut tmp.as_mut().unwrap().next;
while head.is_some() {
let mut h = head.unwrap();
let next = h.next.take();
let v = h.val;
if !set.contains(&v) {
current.insert(h);
current = &mut current.as_mut().unwrap().next;
set.insert(v);
}
head = next;
}
tmp.unwrap().next.take()
}
}