daily pastebin goal
65%
SHARE
TWEET

Untitled

a guest Jan 17th, 2019 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #[derive(PartialEq, Eq, Debug)]
  2. pub struct ListNode {
  3.     pub val: i32,
  4.     pub next: Option<Box<ListNode>>,
  5. }
  6.  
  7. impl ListNode {
  8.     #[inline]
  9.     fn new(val: i32) -> Self {
  10.         ListNode { next: None, val }
  11.     }
  12. }
  13.  
  14. // two-pointer sliding window
  15. impl Solution {
  16.     pub fn remove_nth_from_end(head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> {
  17.         let mut dummy_head = Some(Box::new(ListNode { val: 0, next: head }));
  18.         let mut start = dummy_head.as_ref();
  19.         let mut end = dummy_head.as_ref();
  20.         for _ in 0..n {
  21.             end = end.unwrap().next.as_ref();
  22.         }
  23.         while end.as_ref().unwrap().next.is_some() {
  24.             end = end.unwrap().next.as_ref();
  25.             start = start.unwrap().next.as_ref();
  26.         }
  27.         // TODO: fix the borrow problem
  28.         // ERROR!
  29.         // start.unwrap().next = start.unwrap().next.unwrap().next.take();
  30.         dummy_head.unwrap().next
  31.     }
  32. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top