Coding interview problems โบ Linked Lists
๐ Linked Lists Medium
Remove Nth Node From End of List
Remove the Nth node from the end of a linked list in one pass: open an n-node gap between two pointers, then move both. Tested Python, live visual.
Also searched as: delete nth node from end of linked list ยท two pointer gap technique linked list ยท nth node from the end in one pass
Try it live Free
Linked Lists: Reverse & Floyd's Cycle
Use the linked list lab to get comfortable re-pointing next links; this problem adds a second pointer that trails the first by a fixed gap.
The problem
Given the head of a linked list and an integer n, remove the n-th node counting from the end and return the head of the updated list. You may assume 1 <= n <= the list length.
The 2nd node from the end is 4.
The key insight
Put two pointers n nodes apart and move them together. When the front one reaches the last node, the back one is sitting just before the node to delete. Starting both at a dummy node before the head means deleting the head itself needs no special case.
Step-by-step approach
- Create
dummywithdummy.next = head; setfast = slow = dummy. - Move
fastforwardnsteps. - Move both pointers one step at a time until
fast.nextisNone. - Now
slow.nextis the node to remove: setslow.next = slow.next.next. - Return
dummy.next, which may differ from the original head.
Python solution
class ListNode:
def __init__(self, val=0, next=None):
self.val, self.next = val, next
def remove_nth_from_end(head, n):
dummy = ListNode(0, head)
fast = slow = dummy
for _ in range(n): # open a gap of n nodes
fast = fast.next
while fast.next: # fast stops on the last node
fast, slow = fast.next, slow.next
slow.next = slow.next.next # unlink the n-th node from the end
return dummy.nextComplexity
Where L is the list length, fast walks the list once and slow trails it, so it is one pass with two pointers of storage.
Common mistakes
- Starting without a dummy node, then crashing or returning the wrong head when
nequals the length and the head itself must go. - Stopping when
fastisNoneinstead of whenfast.nextisNone, which leavesslowon the node to delete rather than the one before it. - Returning the original
headinstead ofdummy.next.
Frequently asked questions
What is the time and space complexity?
O(L) time for one pass over a list of length L, and O(1) extra space.
Why use a dummy node?
When the node to remove is the head, there is no node before it. The dummy node provides one, so the same unlink line works in every case.
How do I just find the n-th node from the end without removing it?
Use the same gap: move fast n steps from the head, then move both until fast is None; slow is then the n-th node from the end.
Is the two-pass solution acceptable?
It is correct and also O(L). Interviewers often ask for one pass as a follow-up, which is what the gap technique gives.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Linked Lists Linked Lists: Reverse & Floyd's Cycle lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.