- Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathremove-nodes-from-linked-list.rs
46 lines (38 loc) · 1.09 KB
/
remove-nodes-from-linked-list.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
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fnmain(){}
structSolution;
// Definition for singly-linked list.
#[derive(PartialEq,Eq,Clone,Debug)]
pubstructListNode{
pubval:i32,
pubnext:Option<Box<ListNode>>,
}
implListNode{
#[inline]
fnnew(val:i32) -> Self{
ListNode{next:None, val }
}
}
implSolution{
pubfnremove_nodes(head:Option<Box<ListNode>>) -> Option<Box<ListNode>>{
letmut head = head;
letmut stack = vec![];
whileletSome(mut x) = head.take(){
whileletSome(m) = stack.pop(){
if m >= x.val{
stack.push(m);
break;
}
}
stack.push(x.val);
head = x.next.take();
}
letmut pre_head = ListNode{val:0,next:None};
letmut current = &mut pre_head.next;
for x in stack {
current.insert(Box::new(ListNode{val: x,next:None}));
current = &mut current.as_mut().unwrap().next;
}
pre_head.next.take()
}
}