Remove Nth Node From End of List

Medium Linked List Front Back Pointer Original on LeetCode

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.

00:00
Testcase

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.

ApproachTimeSpace
Two passesO(L)O(1)
Optimal (front and back pointers, one pass)O(L)O(1)

1Two passes

TimeO(L)
SpaceO(1)

Count the length L, then remove node L - n + 1 using a dummy node.

  1. Count L.
  2. 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;
    }
}

2Optimal (front and back pointers, one pass)

TimeO(L)
SpaceO(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.

  1. front = back = dummy; move front n + 1 steps.
  2. While front != null, move both.
  3. 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;
    }
}

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.