From f5ed186d8fec3fb89ffa408faddf8246e1080340 Mon Sep 17 00:00:00 2001 From: GraniteMask Date: Wed, 25 Oct 2023 20:55:10 +0530 Subject: [PATCH] added code in python to remove nth node from end of list --- remove_nth_node_from_end_of_list.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 remove_nth_node_from_end_of_list.py diff --git a/remove_nth_node_from_end_of_list.py b/remove_nth_node_from_end_of_list.py new file mode 100644 index 00000000..6b1cf429 --- /dev/null +++ b/remove_nth_node_from_end_of_list.py @@ -0,0 +1,21 @@ +class Solution: + def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: + + dummy = ListNode(0, head) + left = dummy + right = head + + while n > 0 and right: + right = right.next + n -= 1 + + while right is not None: + left = left.next + right = right.next + + + # deletion code + + left.next = left.next.next + + return dummy.next \ No newline at end of file