-
-
Notifications
You must be signed in to change notification settings - Fork 48.7k
Added Remove nth Node from the End problem solution in Linked List #11781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
95d06d9
added remove_nth_node_from_end.py in linked list Data Structure
sshsrijanr fadbe73
refactoring
sshsrijanr 87494b3
adding file to dir
sshsrijanr fba1c5d
refactoring
sshsrijanr e18a95b
Added type hints to Node class and remove function
sshsrijanr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
class Node: | ||
def __init__(self, val): | ||
sshsrijanr marked this conversation as resolved.
Show resolved
Hide resolved
sshsrijanr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.val = val | ||
self.next = None # Specify that next can be a Node or None | ||
|
||
|
||
def remove(head: Node, n: int) -> Node: | ||
sshsrijanr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 # Make sure fast can be None | ||
slow = extra # Make sure slow can be None | ||
|
||
# Move fast ahead by n nodes | ||
for _ in range(n): | ||
if fast is not None: | ||
fast = 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 | ||
|
||
# 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 the new head, which may be None if the list is empty | ||
|
||
|
||
def print_list(head: Node) -> None: | ||
sshsrijanr marked this conversation as resolved.
Show resolved
Hide resolved
sshsrijanr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
curr = head | ||
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.