19. Remove Nth Node From End of List
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem requires us to remove the n-th node from the end of a singly linked list. A key challenge is to locate this node efficiently. A naive approach might involve two passes: one to find the length of the list and another to find the node to remove. The follow-up question encourages a one-pass solution.
Solution
Remove Nth Node From End of List in Python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
# Create a dummy node to handle edge cases, e.g., removing the head.
dummy = ListNode(0, head)
slow = dummy
fast = dummy
# Move the fast pointer n steps ahead.
for _ in range(n):
fast = fast.next
# Move both pointers until the fast pointer reaches the last node.
while fast.next:
slow = slow.next
fast = fast.next
# Now, slow is pointing to the node just before the one to be removed.
slow.next = slow.next.next
# The head of the modified list is dummy.next.
return dummy.next
Remove Nth Node From End of List in Java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
// Create a dummy node to handle edge cases, e.g., removing the head.
ListNode dummy = new ListNode(0, head);
ListNode slow = dummy;
ListNode fast = dummy;
// Move the fast pointer n steps ahead.
for (int i = 0; i < n; i++) {
fast = fast.next;
}
// Move both pointers until the fast pointer reaches the last node.
while (fast.next != null) {
slow = slow.next;
fast = fast.next;
}
// Now, slow is pointing to the node just before the one to be removed.
slow.next = slow.next.next;
// The head of the modified list is dummy.next.
return dummy.next;
}
}
Remove Nth Node From End of List in C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
// Create a dummy node on the stack to simplify edge cases like removing the head.
ListNode dummy(0, head);
ListNode* slow = &dummy;
ListNode* fast = &dummy;
// Move the fast pointer n steps ahead.
for (int i = 0; i < n; ++i) {
fast = fast->next;
}
// Move both pointers until the fast pointer reaches the last node.
while (fast->next != nullptr) {
slow = slow->next;
fast = fast->next;
}
// Now, slow is pointing to the node just before the one to be removed.
// In a real application, one might want to deallocate the removed node.
// ListNode* nodeToDelete = slow->next;
slow->next = slow->next->next;
// delete nodeToDelete;
// The head of the modified list is dummy.next.
return dummy.next;
}
};
Remove Nth Node From End of List in TypeScript
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
// Create a dummy node to handle edge cases, e.g., removing the head.
const dummy = new ListNode(0, head);
let slow: ListNode = dummy;
let fast: ListNode = dummy;
// Move the fast pointer n steps ahead.
// Due to problem constraints (1 <= n <= sz), fast will not be null here.
for (let i = 0; i < n; i++) {
fast = fast.next!;
}
// Move both pointers until the fast pointer reaches the last node.
while (fast.next !== null) {
slow = slow.next!;
fast = fast.next!;
}
// Now, slow is pointing to the node just before the one to be removed.
slow.next = slow.next!.next;
// The head of the modified list is dummy.next.
return dummy.next;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.