FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Added remove nth node from end of a linked-list by omrawal · Pull Request #131 · AllAlgorithms/python · GitHub

This repository was archived by the owner on Sep 7, 2025. It is now read-only.
/ python Public archive
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
32 changes: 32 additions & 0 deletions algorithms/linkedlist/delete_nth_node_from_end.py
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,32 @@
# remove nth node from end
# https://leetcode.com/problems/remove-nth-node-from-end-of-list/

# brute
# create a new linked list without that element
# Time O(n)
# Space O(n)

# optimal


# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next


class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
if(head.next == None):
return None
start = ListNode()
start.next = head
slow = fast = start
for i in range(1, n+1):
fast = fast.next
while(fast.next != None):
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return start.next

Back | FazBrowse Home | New Git URL