Remove Nth Node From End of List
Medium · Linked lists
Jump to codeRemove the nth node counted from the end of a singly linked list and return the new head. Assume 1 ≤ n ≤ the list length.
Examples
Example 1
head = [1,2,3,4,5] n = 2
Output: [1,2,3,5]
Example 2
head = [1] n = 1
Output: []
Constraints
- The number of nodes in the list is sz.
- 1 <= sz <= 30
- 0 <= Node.val <= 100
- 1 <= n <= sz
Function contract
Linked lists are displayed as value arrays. Your code receives ListNode objects with val and next fields.
Need a hint?
Use two pointers separated by a fixed gap.
Real linked list interview questions
Questions on the same topic that candidates reported from real company interviews. Some company questions are locked and open with Premium or XP.
- Flatten a Linked List with Nested Side Branches into One Doubly Linked ListTesla
- Find the Node Where Two Singly Linked Lists MergeLinkedIn
- Remove duplicates from a singly linked listSalesforce
Solution approach
Start both pointers at a dummy node before the head. Advance fast n + 1 links, then advance both until fast is null. Slow now precedes the node to remove. Set slow.next to slow.next.next and return dummy.next; this also handles removing the head.
Complexity
O(n) time and O(1) space, where n here denotes list length.
Python reference solution
def removeNthFromEnd(head, n):
dummy = ListNode(0, head)
fast = slow = dummy
for _ in range(n):
fast = fast.next
while fast.next:
fast, slow = fast.next, slow.next
slow.next = slow.next.next
return dummy.next
JavaScript solution
function removeNthFromEnd(head, n) {
const dummy = new ListNode(0, head);
let fast = dummy, slow = dummy;
for (let i = 0; i < n; i++) fast = fast.next;
while (fast.next) {
fast = fast.next; slow = slow.next;
}
slow.next = slow.next.next;
return dummy.next;
}
Java solution
import java.util.*;
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0, head), fast = dummy, 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;
return dummy.next;
}
}
C++ solution
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode dummy(0, head);
ListNode *fast = &dummy, *slow = &dummy;
for (int i = 0; i < n; i++) fast = fast->next;
while (fast->next) {
fast = fast->next; slow = slow->next;
}
slow->next = slow->next->next;
return dummy.next;
}
};
Trace a boundary case, then implement the approach in the editor. Submit checks your solution against all 7 test cases.
Keep your reasoning, pseudocode, and edge cases here.
Notes stay in this browser when storage is available.
Accepted submissions mark this problem complete. Progress stays in this browser.
[
[
1,
2,
3,
4,
5
],
2
][ 1, 2, 3, 5 ]
[
[
1
],
1
][]
[
[
1,
2
],
1
][ 1 ]
[
[
1,
2,
3
],
3
][ 2, 3 ]
[
[
1,
2,
3,
4
],
2
][ 1, 2, 4 ]
Drafts are saved per language in this browser when storage is available.