Unsafe rust single pass solution
The fastest solution is to use two pointers, one in front and the other n step back. When the front pointer hits the end of the list, the tail pointer points at
The fastest solution is to use two pointers, one in front and the other n step back. When the front pointer hits the end of the list, the tail pointer points at the node to remove. So the front pointer can be immutable while the tail one must be mutable. \n\nHowever, we cannot have both an immutable and a mutable reference to the same object in safe rust --- I spent quite some time fighting with the borrow checker and realised this is impossible (correct me if I\'m wrong) --- so the safe rust solution requires two passes: one for the length of list, the other for removing the node.\n\nHere is a solution with unsafe rust which should be the most performant.\n```\nimpl Solution {\n    pub fn remove_nth_from_end(head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> {\n        unsafe {\n            let mut head = head;\n            let mut front: *mut Option<Box<ListNode>> = &mut head;\n            let mut tail: *mut Option<Box<ListNode>> = &mut head;\n            for _ in 0..n {\n                front = &mut (*front).as_mut().unwrap().next;\n            }\n            if (*front).is_none() {\n                return head.take().unwrap().next;\n            }\n            loop {\n                front = &mut (*front).as_mut().unwrap().next;\n                if (*front).is_none() {\n                    break;\n                }\n                tail = &mut (*tail).as_mut().unwrap().next;\n            }\n            (*tail).as_mut().unwrap().next = (*tail).as_mut().unwrap().next.as_mut().unwrap().next.take();\n            head\n        }\n    }\n}\n```
[]