From 95d06d95f4243287e897a8bfad57773a03cbd0d6 Mon Sep 17 00:00:00 2001 From: sshsrijanr Date: Sat, 5 Oct 2024 20:13:43 +0530 Subject: [PATCH 1/5] added remove_nth_node_from_end.py in linked list Data Structure --- .../linked_list/remove_nth_node_from_end.py | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 data_structures/linked_list/remove_nth_node_from_end.py diff --git a/data_structures/linked_list/remove_nth_node_from_end.py b/data_structures/linked_list/remove_nth_node_from_end.py new file mode 100644 index 000000000000..c6698378e72e --- /dev/null +++ b/data_structures/linked_list/remove_nth_node_from_end.py @@ -0,0 +1,37 @@ +class Node(): + + def __init__(self, val): + self.val = val + self.next = None + + +def remove(head, n): + extra = Node(0) + extra.next = head + fast = extra + slow = extra + for _ in range(n): + fast = fast.next + + while fast.next: + fast = fast.next + slow = slow.next + + slow.next = slow.next.next + + return extra.next + +def print_list(head): + curr = head + while curr: + print(curr.val, end=" ") + curr = curr.next + +head = Node(1) +head.next = Node(2) +head.next.next = Node(3) +head.next.next.next = Node(4) +head.next.next.next.next = Node(5) +print_list(head) +head = remove(head, 5) +print_list(head) From fadbe730fd228a32540ea33378f96e9cf7c3b5b6 Mon Sep 17 00:00:00 2001 From: sshsrijanr Date: Sat, 5 Oct 2024 20:22:08 +0530 Subject: [PATCH 2/5] refactoring --- data_structures/linked_list/remove_nth_node_from_end.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/data_structures/linked_list/remove_nth_node_from_end.py b/data_structures/linked_list/remove_nth_node_from_end.py index c6698378e72e..e261a66568c6 100644 --- a/data_structures/linked_list/remove_nth_node_from_end.py +++ b/data_structures/linked_list/remove_nth_node_from_end.py @@ -1,5 +1,4 @@ -class Node(): - +class Node: def __init__(self, val): self.val = val self.next = None @@ -21,12 +20,14 @@ def remove(head, n): return extra.next + def print_list(head): curr = head while curr: print(curr.val, end=" ") curr = curr.next + head = Node(1) head.next = Node(2) head.next.next = Node(3) From 87494b36b29f0ad45bc5c526dfe39b3f3a5b5532 Mon Sep 17 00:00:00 2001 From: sshsrijanr Date: Sat, 5 Oct 2024 22:01:08 +0530 Subject: [PATCH 3/5] adding file to dir --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index cdbbac684fd2..a28f897a4bee 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -265,6 +265,7 @@ * [Merge Two Lists](data_structures/linked_list/merge_two_lists.py) * [Middle Element Of Linked List](data_structures/linked_list/middle_element_of_linked_list.py) * [Print Reverse](data_structures/linked_list/print_reverse.py) + * [Remove nth Node from the End](data_structures/linked_list/remove_nth_node_from_end.py) * [Reverse K Group](data_structures/linked_list/reverse_k_group.py) * [Rotate To The Right](data_structures/linked_list/rotate_to_the_right.py) * [Singly Linked List](data_structures/linked_list/singly_linked_list.py) From fba1c5d64384262ba5872ea3ef9afc775fbe8059 Mon Sep 17 00:00:00 2001 From: sshsrijanr Date: Sat, 5 Oct 2024 22:42:40 +0530 Subject: [PATCH 4/5] refactoring --- data_structures/linked_list/remove_nth_node_from_end.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/remove_nth_node_from_end.py b/data_structures/linked_list/remove_nth_node_from_end.py index e261a66568c6..107832a608b8 100644 --- a/data_structures/linked_list/remove_nth_node_from_end.py +++ b/data_structures/linked_list/remove_nth_node_from_end.py @@ -21,7 +21,7 @@ def remove(head, n): return extra.next -def print_list(head): +def print_list(head: Node) -> None: curr = head while curr: print(curr.val, end=" ") From e18a95b26fb9ab14a351e97646f3a71561bef9f3 Mon Sep 17 00:00:00 2001 From: sshsrijanr Date: Sat, 5 Oct 2024 22:54:21 +0530 Subject: [PATCH 5/5] Added type hints to Node class and remove function --- .../linked_list/remove_nth_node_from_end.py | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/data_structures/linked_list/remove_nth_node_from_end.py b/data_structures/linked_list/remove_nth_node_from_end.py index 107832a608b8..3b197a34acce 100644 --- a/data_structures/linked_list/remove_nth_node_from_end.py +++ b/data_structures/linked_list/remove_nth_node_from_end.py @@ -1,38 +1,58 @@ class Node: def __init__(self, val): self.val = val - self.next = None + self.next = None # Specify that next can be a Node or None -def remove(head, n): +def remove(head: Node, n: int) -> Node: + if head is None: + return None # If the head is None, return None + + # Create a dummy node that points to the head extra = Node(0) extra.next = head - fast = extra - slow = extra + fast = extra # Make sure fast can be None + slow = extra # Make sure slow can be None + + # Move fast ahead by n nodes for _ in range(n): - fast = fast.next + if fast is not None: + fast = fast.next - while fast.next: + # Move until fast reaches the end + while fast is not None and fast.next is not None: fast = fast.next slow = slow.next - slow.next = slow.next.next + # Remove the nth node from the end + if slow is not None and slow.next is not None: + slow.next = slow.next.next # Link slow to the next of the node to be deleted - return extra.next + return extra.next # Return the new head, which may be None if the list is empty def print_list(head: Node) -> None: curr = head - while curr: + while curr is not None: print(curr.val, end=" ") curr = curr.next + print() # for a new line +# Creating the linked list 1 -> 2 -> 3 -> 4 -> 5 head = Node(1) head.next = Node(2) head.next.next = Node(3) head.next.next.next = Node(4) head.next.next.next.next = Node(5) + +# Print the original list +print("Original list:") print_list(head) + +# Remove the 5th node from the end (the head in this case) head = remove(head, 5) + +# Print the modified list +print("List after removing the 5th node from the end:") print_list(head)