-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathremove-duplicates-from-sorted-list-ii.rs
52 lines (46 loc) · 1.3 KB
/
remove-duplicates-from-sorted-list-ii.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
42
43
44
45
46
47
48
49
50
51
52
#![allow(dead_code, unused, unused_variables)]
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 delete_duplicates(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let s = Self::func(head);
s.0
}
fn func(mut head: Option<Box<ListNode>>) -> (Option<Box<ListNode>>, Option<i32>) {
if head.is_none() {
return (None, None);
}
let v = head.as_ref().unwrap().val;
let (node, val) = Self::func(head.as_mut().unwrap().next.take());
if val.is_none() {
(head, Some(v))
} else {
if val.unwrap() == v {
if node.is_none() {
(None, Some(v))
} else {
if node.as_ref().unwrap().val == v {
(node.unwrap().next, Some(v))
} else {
(node, Some(v))
}
}
} else {
head.as_mut().unwrap().next = node;
(head, Some(v))
}
}
}
}