diff --git a/Q2/solution.py b/Q2/solution.py index dd114fb..5bfec75 100644 --- a/Q2/solution.py +++ b/Q2/solution.py @@ -5,4 +5,10 @@ # self.next = next class Solution: def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: + fast, slow = head, head + for _ in range(n): fast = fast.next + if not fast: return head.next + while fast.next: fast, slow = fast.next, slow.next + slow.next = slow.next.next + return head