Java Linked List Program · OT43
Remove the Nth Node from the End of a Linked List in Java
Start a fast pointer n steps ahead of a slow pointer. When the fast pointer reaches the last node, the slow pointer stands right before the node to remove.
Problem Statement
Given the head of a singly linked list and an integer n, remove the n-th node from the end and return the new head.
n is between 1 and the length of the list, so removing the head is possible.
Input/Output Examples
1→2→3→4→5, n = 2Output1→2→3→5Explanation: 4 is second from the end.1→2→3→4→5, n = 5Output2→3→4→5Explanation: the first node is removed.8, n = 1Output(empty)Explanation: the list becomes empty.Visual Explanation
- Gap of 2pointersslow: dummyfast: 2move fast n = 2 steps
- Move bothpointersslow: 3fast: 5until fast.next is null
- Unlinklist12345slow.next = slow.next.next
- Done1→2→3→5
The dummy node in front of the head means the node before the target always exists, even when the target is the head itself.
Approaches
Both approaches use a dummy head. One keeps two pointers n apart; the other counts the length first.
Approach 1: Two Pointers, One Pass (Recommended)
Idea: move fast n steps ahead, then move both until fast.next is null; slow.next is the target.
- Create a dummy node pointing to the head.
- Move
fastnsteps from the dummy. - Move both pointers until
fastis on the last node. - Set
slow.next = slow.next.nextand returndummy.next.
Time: O(n) · Space: O(1).
Approach 2: Count, Then Remove
Idea: count the nodes, then walk to the node before position length - n and unlink the next one.
- Pass 1: count the length.
- Pass 2: from the dummy, take
length - nsteps. - Unlink the following node.
Time: O(n) · Space: O(1).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Two Pointers, One Pass (Recommended)
A single pass, which is the usual follow-up requirement.
public class RemoveNthOnePass {
static class Node {
int val;
Node next;
Node(int val) { this.val = val; }
}
static Node of(int... values) {
Node dummy = new Node(0);
Node t = dummy;
for (int v : values) {
t = t.next = new Node(v);
}
return dummy.next;
}
static String show(Node n) {
if (n == null) {
return "(empty)";
}
StringBuilder sb = new StringBuilder();
for (; n != null; n = n.next) {
sb.append(n.val).append(n.next != null ? "->" : "");
}
return sb.toString();
}
// Move fast n steps ahead; when fast reaches the end, slow stands just before the target
static Node removeNthFromEnd(Node head, int n) {
Node dummy = new Node(0);
dummy.next = head;
Node fast = dummy;
Node slow = dummy;
for (int i = 0; i < n; i++) {
fast = fast.next;
}
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next; // unlink the target node
return dummy.next;
}
public static void main(String[] args) {
System.out.println("1->2->3->4->5, n=2 -> " + show(removeNthFromEnd(of(1, 2, 3, 4, 5), 2)));
System.out.println("1->2->3->4->5, n=5 -> " + show(removeNthFromEnd(of(1, 2, 3, 4, 5), 5)));
System.out.println("8, n=1 -> " + show(removeNthFromEnd(of(8), 1)));
}
}
1->2->3->4->5, n=2 -> 1->2->3->5 1->2->3->4->5, n=5 -> 2->3->4->5 8, n=1 -> (empty)
Time complexity: O(n) · Space complexity: O(1).
Approach 2: Count, Then Remove
Two passes, but the arithmetic is easy to check.
public class RemoveNthTwoPass {
static class Node {
int val;
Node next;
Node(int val) { this.val = val; }
}
static Node of(int... values) {
Node dummy = new Node(0);
Node t = dummy;
for (int v : values) {
t = t.next = new Node(v);
}
return dummy.next;
}
static String show(Node n) {
if (n == null) {
return "(empty)";
}
StringBuilder sb = new StringBuilder();
for (; n != null; n = n.next) {
sb.append(n.val).append(n.next != null ? "->" : "");
}
return sb.toString();
}
// Count the nodes, then remove the node at index (length - n) from the front
static Node removeNthFromEnd(Node head, int n) {
int length = 0;
for (Node t = head; t != null; t = t.next) {
length++;
}
Node dummy = new Node(0);
dummy.next = head;
Node prev = dummy;
for (int i = 0; i < length - n; i++) {
prev = prev.next;
}
prev.next = prev.next.next;
return dummy.next;
}
public static void main(String[] args) {
System.out.println("1->2->3->4->5, n=2 -> " + show(removeNthFromEnd(of(1, 2, 3, 4, 5), 2)));
System.out.println("1->2->3->4->5, n=5 -> " + show(removeNthFromEnd(of(1, 2, 3, 4, 5), 5)));
System.out.println("8, n=1 -> " + show(removeNthFromEnd(of(8), 1)));
}
}
1->2->3->4->5, n=2 -> 1->2->3->5 1->2->3->4->5, n=5 -> 2->3->4->5 8, n=1 -> (empty)
Time complexity: O(n) · Space complexity: O(1).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Two Pointers, One Pass | O(n) | O(1) |
| Count, Then Remove | O(n) | O(1) |
Here n is the number of nodes (the list length). Both approaches are linear with constant extra memory.
Interview Tips and Recommended Approach
- Use a dummy node. It removes the special case of deleting the head.
- Stop on
fast.next, notfast, so slow stops one node early. - Validate
nif the input is not guaranteed to be in range. - Reuse the gap idea. Two pointers a fixed distance apart also find the k-th node from the end.
Recommended Approach: Two Pointers, One Pass
Why: it removes the node in a single pass with constant memory, and the dummy node keeps every case, including the head, uniform.