-
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)]
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_nodes(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut head = head;
let mut stack = vec![];
while let Some(mut x) = head.take() {
while let Some(m) = stack.pop() {
if m >= x.val {
stack.push(m);
break;
}
}
stack.push(x.val);
head = x.next.take();
}
let mut pre_head = ListNode { val: 0, next: None };
let mut 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()
}
}