Remove Nth Node from End of List in DSA Javascript - Time & Space Complexity
We want to understand how the time to remove the nth node from the end of a linked list changes as the list grows.
How does the number of steps grow when the list gets longer?
Analyze the time complexity of the following code snippet.
function removeNthFromEnd(head, n) {
let fast = head;
let slow = head;
for (let i = 0; i < n; i++) {
fast = fast.next;
}
while (fast) {
fast = fast.next;
slow = slow.next;
}
if (!fast) {
return head.next;
}
slow.next = slow.next.next;
return head;
}
This code removes the nth node from the end of a singly linked list by using two pointers moving through the list.
- Primary operation: Two loops moving pointers through the list.
- How many times: First loop runs n times; second loop runs (length - n) times.
As the list size grows, the total steps grow roughly in a straight line with the list length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The steps increase directly with the list size, so doubling the list roughly doubles the work.
Time Complexity: O(n)
This means the time to remove the nth node from the end grows linearly with the list size.
[X] Wrong: "The operation takes constant time because we only remove one node."
[OK] Correct: Even though only one node is removed, we must first find the node by traversing the list, which takes time proportional to the list length.
Understanding this linear time complexity helps you explain how to efficiently handle linked list operations, a common topic in interviews.
"What if we used a doubly linked list with a tail pointer? How would the time complexity change?"
Practice
Remove Nth Node from End of List ask you to do?Solution
Step 1: Understand the problem statement
The problem asks to remove a node counting from the end of a linked list, not from the start.Step 2: Identify the correct action
Removing the nth node from the end means deleting that specific node and linking its previous node to the next node.Final Answer:
Delete the node that is the nth from the end in a linked list -> Option AQuick Check:
Remove nth from end = Delete nth from end [OK]
- Confusing nth from end with nth from start
- Trying to reverse the list instead of removing
- Removing the wrong node index
Solution
Step 1: Review JavaScript function declaration styles
JavaScript supports function declarations, function expressions, and arrow functions.Step 2: Check each option
function removeNthFromEnd(head, n) { /* code */ } is a function declaration, B is an arrow function, C is a function expression. All are valid ways to declare functions.Final Answer:
All of the above -> Option CQuick Check:
All function styles valid in JS [OK]
- Thinking only one function syntax is valid
- Confusing arrow functions with older syntax
- Missing const or function keyword
class ListNode {
constructor(val, next = null) {
this.val = val;
this.next = next;
}
}
function removeNthFromEnd(head, n) {
let dummy = new ListNode(0, head);
let first = dummy;
let second = dummy;
for (let i = 0; i <= n; i++) {
first = first.next;
}
while (first !== null) {
first = first.next;
second = second.next;
}
second.next = second.next.next;
return dummy.next;
}
// List: 1 -> 2 -> 3 -> 4 -> 5 -> null
let head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
let newHead = removeNthFromEnd(head, 2);
// Print list
let result = [];
while (newHead) {
result.push(newHead.val);
newHead = newHead.next;
}
console.log(result);Solution
Step 1: Understand the code logic
The code uses two pointers with a dummy node to remove the nth node from the end. Here, n=2 means remove the 2nd last node (value 4).Step 2: Trace the linked list after removal
Original list: 1 -> 2 -> 3 -> 4 -> 5. Removing node with value 4 results in 1 -> 2 -> 3 -> 5.Final Answer:
[1, 2, 3, 5] -> Option DQuick Check:
Remove 2nd last node = remove 4 [OK]
- Removing the wrong node counting from start
- Not using dummy node causing errors on head removal
- Off-by-one error in loop for pointer movement
function removeNthFromEnd(head, n) {
let dummy = new ListNode(0, head);
let first = dummy;
let second = dummy;
for (let i = 0; i < n; i++) {
first = first.next;
}
while (first.next !== null) {
first = first.next;
second = second.next;
}
second.next = second.next.next;
return dummy.next;
}Solution
Step 1: Analyze the for loop pointer movement
The for loop moves the first pointer n times, which is correct to create a gap of n nodes between first and second pointers.Step 2: Understand the while loop condition
The while loop condition should befirst !== nullto ensure the first pointer reaches the end, allowing the second pointer to point to the node before the target.Final Answer:
The while loop condition should be first !== null instead of first.next !== null -> Option AQuick Check:
Use while (first !== null) to correctly position second pointer [OK]
- Moving first pointer only n times instead of n+1
- Incorrect while loop condition causing null errors
- Removing dummy node mistakenly
1 -> 2 -> 3 -> 4 -> 5 -> null, what will be the resulting list after removing the 5th node from the end using the standard two-pointer approach?Solution
Step 1: Identify the node to remove
Removing the 5th node from the end means removing the first node (value 1) because the list length is 5.Step 2: Remove the head node and link next node as new head
After removal, the list starts from node with value 2: 2 -> 3 -> 4 -> 5.Final Answer:
[2, 3, 4, 5] -> Option BQuick Check:
Remove 5th from end = remove head [OK]
- Not handling removal of head node correctly
- Returning original list without removal
- Confusing nth from end with nth from start
Ready to prove it?
Pass the test and this pattern moves unknown
