Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2625 from rmrt1n/019
Browse files Browse the repository at this point in the history
Create 0019-remove-nth-node-from-end-of-list.rs
  • Loading branch information
tahsintunan committed Jun 23, 2023
2 parents 908bc27 + 8870d1c commit e5dbf18
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions rust/0019-remove-nth-node-from-end-of-list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
impl Solution {
pub fn remove_nth_from_end(head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> {
let mut dummy = Box::new(ListNode { val: 0, next: head.clone() });
let (mut left, mut right) = (dummy.as_mut(), head);

let mut n = n;
while n > 0 && right.is_some() {
right = right.unwrap().next;
n -= 1;
}

while let Some(r) = right {
left = left.next.as_mut().unwrap();
right = r.next;
}

left.next = left.next.take().unwrap().next.take();

dummy.next
}
}

0 comments on commit e5dbf18

Please sign in to comment.