From bf6508c990ede3174ea0e9f4940ee6b33beac933 Mon Sep 17 00:00:00 2001 From: harisjamalkhan111 <90018285+harisjamalkhan111@users.noreply.github.com> Date: Sun, 18 Sep 2022 06:50:31 -0700 Subject: [PATCH] Q2 solution submit by Haris Jamal Khan I have used two pointers by giving the first pointer (fast) a head start before starting the second pointer (slow). Doing this will cause slow to reach the n'th node from the end at the same time that fast reaches the end. --- Q2/solution.py | 6 ++++++ 1 file changed, 6 insertions(+) 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