Skip to content

Commit e5dbf18

Browse files
authored
Merge pull request #2625 from rmrt1n/019
Create 0019-remove-nth-node-from-end-of-list.rs
2 parents 908bc27 + 8870d1c commit e5dbf18

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
pub fn remove_nth_from_end(head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> {
3+
let mut dummy = Box::new(ListNode { val: 0, next: head.clone() });
4+
let (mut left, mut right) = (dummy.as_mut(), head);
5+
6+
let mut n = n;
7+
while n > 0 && right.is_some() {
8+
right = right.unwrap().next;
9+
n -= 1;
10+
}
11+
12+
while let Some(r) = right {
13+
left = left.next.as_mut().unwrap();
14+
right = r.next;
15+
}
16+
17+
left.next = left.next.take().unwrap().next.take();
18+
19+
dummy.next
20+
}
21+
}

0 commit comments

Comments
 (0)