Meta Description: Master the efficient two-pointer method to remove the Nth node from the end of a linked list. Includes step-by-step Java code explanation and edge case handling for LeetCode Problem 19.
🧠 Problem Summary
You’re given the head
of a singly linked list. Your task is to remove the Nth node from the end of the list and return the updated head. This is a classic linked list problem and frequently asked in coding interviews.
🔍 Example
Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
Explanation: The node with value 4
(2nd from the end) is removed.
✅ Why Use the Two-Pointer Technique?
A naive approach involves two passes:
- Count the total nodes.
- Delete the
(length - n)
th node.
But we can do better.
Using the two-pointer technique, we remove the target node in one pass, resulting in:
- Time Complexity: O(N)
- Space Complexity: O(1)
This is ideal for large linked lists and optimal performance.
🧩 Step-by-Step Algorithm
🔗 Core Steps:
- Initialize two pointers:
slow
andfast
, both pointing to the head. - Advance
fast
byn
steps to create a gap ofn
nodes. - Move both pointers simultaneously until
fast
reaches the end. - Remove the target node by adjusting
slow.next
.
💻 Java Code Implementation
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if (head == null || head.next == null) return null;
ListNode slow = head, fast = head;
// Move fast pointer n steps ahead
while (n-- > 0) {
fast = fast.next;
}
// Edge case: removing the head node
if (fast == null) {
return head.next;
}
// Move both pointers until fast reaches the last node
while (fast.next != null) {
slow = slow.next;
fast = fast.next;
}
// Skip the Nth node from the end
slow.next = slow.next.next;
return head;
}
}
🧠 Code Walkthrough
🔍 1. Edge Case Handling
- If the list has only one node, removing it returns
null
. - If
n
equals the length of the list, thenfast
becomesnull
aftern
steps — which means we need to remove thehead
.
🏃 2. Fast-Slow Traversal
- The
fast
pointer movesn
steps ahead, creating a buffer. - Once
fast
hits the end,slow
is right before the node to remove.
🧹 3. Node Deletion
slow.next = slow.next.next
effectively skips over the target node, deleting it from the list.
📈 Complexity Analysis
Metric | Value |
---|---|
Time | O(N) – One full traversal |
Space | O(1) – No extra memory used |
🔍 Visual Example
Consider: head = [1 → 2 → 3 → 4 → 5]
, n = 2
- After moving
fast
2 steps:fast
= node3
,slow
= node1
- Move both pointers until
fast
reaches the end:fast
= node5
,slow
= node3
- Delete
slow.next
(node4
):
Resulting list =[1 → 2 → 3 → 5]
🧠 Final Thoughts
The two-pointer method is a powerful and elegant technique for solving many linked list problems. For LeetCode 19, it lets us remove the Nth node from the end in a single pass, making it a go-to strategy in coding interviews.
🧪 Practice More Linked List Problems
- 🔗 Middle of the Linked List (LeetCode 876)
- 🔗 Linked List Cycle II (LeetCode 142)
- 🔗 Remove Duplicates from Sorted List II (LeetCode 82)
Got any questions? Confused about edge cases? Drop your doubts below — let’s learn together! 🚀
Keywords: remove nth node from end of linked list, linked list deletion Java, two-pointer technique, LeetCode 19 Java solution, optimal linked list algorithm, coding interview prep, linked list Java code.