Remove Nth Node From End of List
Delete the n-th node from the end in one pass, with two pointers kept n nodes apart and a dummy head.
The problem
You get the head of a linked list and an integer n. Remove the n-th node counted from the end of the list, where n = 1 is the last node, and return the head of the changed list. You can assume n is between 1 and the length of the list.
Input head = [6, 2, 8, 3, 5], n = 3
Output [6, 2, 3, 5]
Counting from the end, 5 is first, 3 second and 8 third, so 8 is removed.
Input head = [4, 9], n = 2
Output [9]
The node to remove is the head itself, so the answer starts at 9.
Key constraint: The follow-up asks for a single pass, so you can't count the length first and then walk again.
How to spot the pattern
- A position is given from the end of a singly linked list, which you can only walk forwards.
- Two pointers kept a fixed distance apart turn "n from the end" into "when the front pointer reaches the end".
The idea
If one pointer starts n nodes ahead of another and both move at the same speed, the gap between them stays n. So when the front pointer reaches the last node, the back pointer sits just before the node to remove. Starting both at a dummy node placed in front of the head makes removing the head work exactly like removing any other node.
Brute force: Count the length in a first pass, then walk to the node before position length - n in a second pass; it is O(L) too, but takes two passes.
Step by step
- 1Create a dummy node whose
nextishead, and startfastandslowat the dummy. - 2Move
fastforwardnnodes. - 3Move both pointers one node at a time until
fast.nextis null;slownow sits just before the node to remove. - 4Unlink that node with
slow.next = slow.next.nextand returndummy.next.
See the pattern move
The linked lists pattern on a small example. Step through it, or press play.
Code
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
dummy = ListNode(0, head)
fast = slow = dummy
for _ in range(n):
fast = fast.next # fast is now n nodes ahead of slow
while fast.next:
fast = fast.next
slow = slow.next
slow.next = slow.next.next # slow sits just before the node to remove
return dummy.nextComplexity
Time
O(L)
Space
O(1)
L is the number of nodes: fast walks the list once and slow follows behind it, using only a dummy node and two pointers.
Watch out for
- Skipping the dummy node, so removing the head (when
nequals the length) needs a special case that is easy to forget. - Getting the gap off by one, so
slowstops on the node to remove instead of the node before it; check your loop on a two-node list. - Returning
headinstead ofdummy.next, which is wrong when the head itself was removed.
Follow-up questions
How would you return the n-th node from the end without removing it?
Use the same gap, but start both pointers at head and move them until fast becomes null; slow is then the node itself.
What if n could be larger than the length of the list?
While moving fast ahead, check for null; if the list ends before n steps, there is nothing to remove, so return head unchanged.
In Hinglish
Fast pointer ko n kadam aage bhejo, phir dono ko saath chalao. Fast end par pahunche tab slow theek hataye jaane wale node se pehle khada hoga.