Remove Nth Node From End of List
Remove Nth Node From End of List is a medium linked list problem solved with the front back pointer pattern.
The best approach, optimal (front and back pointers, one pass), runs in O(L) time and O(1) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from two passes up.
Problem
Remove the n-th node from the end of a linked list and return its head.
Follow-up: can you do it in one pass?
Examples
Example 1
- Input
head = [1, 2, 3, 4, 5], n = 2- Output
[1, 2, 3, 5]
Example 2
- Input
head = [1], n = 1- Output
[]
Constraints
1 <= n <= size <= 30- Follow-up: one pass.
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (2 examples, or your own). Submit also runs 9 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Two passes | O(L) | O(1) |
| Optimal (front and back pointers, one pass) | O(L) | O(1) |
1Two passes
O(L)O(1)Count the length L, then remove node L - n + 1 using a dummy node.
- Count L.
- From dummy, walk L - n steps and unlink the next node.
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
int len = 0;
for (ListNode p = head; p != null; p = p.next) len++;
ListNode dummy = new ListNode(0, head), prev = dummy;
for (int i = 0; i < len - n; i++) prev = prev.next;
prev.next = prev.next.next;
return dummy.next;
}
}class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
length = 0
p = head
while p:
length += 1
p = p.next
dummy = ListNode(0, head)
prev = dummy
for _ in range(length - n):
prev = prev.next
prev.next = prev.next.next
return dummy.nextclass Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
int len = 0;
for (ListNode* p = head; p != nullptr; p = p->next) len++;
ListNode* dummy = new ListNode(0, head);
ListNode* prev = dummy;
for (int i = 0; i < len - n; i++) prev = prev->next;
prev->next = prev->next->next;
return dummy->next;
}
};function removeNthFromEnd(head, n) {
let len = 0;
for (let p = head; p !== null; p = p.next) len++;
const dummy = new ListNode(0, head);
let prev = dummy;
for (let i = 0; i < len - n; i++) prev = prev.next;
prev.next = prev.next.next;
return dummy.next;
}2Optimal (front and back pointers, one pass)
O(L)O(1)Advance front n + 1 steps from a dummy node, then move front and back together until front is null. back is now just before the node to remove.
- front = back = dummy; move front n + 1 steps.
- While front != null, move both.
- back.next = back.next.next.
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0, head), front = dummy, back = dummy;
for (int i = 0; i <= n; i++) front = front.next;
while (front != null) { front = front.next; back = back.next; }
back.next = back.next.next;
return dummy.next;
}
}class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
dummy = ListNode(0, head)
front, back = dummy, dummy
for _ in range(n + 1):
front = front.next
while front:
front = front.next
back = back.next
back.next = back.next.next
return dummy.nextclass Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummy = new ListNode(0, head);
ListNode* front = dummy;
ListNode* back = dummy;
for (int i = 0; i <= n; i++) front = front->next;
while (front != nullptr) { front = front->next; back = back->next; }
back->next = back->next->next;
return dummy->next;
}
};function removeNthFromEnd(head, n) {
const dummy = new ListNode(0, head);
let front = dummy, back = dummy;
for (let i = 0; i <= n; i++) front = front.next;
while (front !== null) { front = front.next; back = back.next; }
back.next = back.next.next;
return dummy.next;
}Edge cases to test
- Removing the head (n equals the length)
- Single-node list
Hints
Hint 1
Put the front pointer n steps ahead, then move both. Start from a dummy so the back pointer stops before the node to remove.
FAQ
What is the best time complexity for Remove Nth Node From End of List?
Optimal (front and back pointers, one pass) runs in O(L) time and O(1) extra space.
Which pattern does Remove Nth Node From End of List use?
It is a linked list problem that uses the front back pointer pattern.
Is there a brute force solution for Remove Nth Node From End of List?
Yes. Two passes takes O(L) time and O(1) space. Count the length L, then remove node L - n + 1 using a dummy node.
Which edge cases should I test for Remove Nth Node From End of List?
Removing the head (n equals the length); Single-node list.