Bird
Raised Fist0
DSA Pythonprogramming

Remove Nth Node from End of List in DSA Python

Choose your learning style10 modes available
Mental Model
To remove the nth node from the end, we find the node just before it by moving two pointers with a gap of n between them.
Analogy: Imagine two friends walking on a path where one starts n steps ahead. When the front friend reaches the end, the other is right before the target friend to be removed.
head -> 1 -> 2 -> 3 -> 4 -> 5 -> null
          ↑
        slow
                 ↑
               fast
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> 4 -> 5, remove 2nd node from end
Goal: Remove the 2nd node from the end, which is node with value 4
Step 1: Move fast pointer n=2 steps ahead
head -> 1 -> 2 -> 3 -> 4 -> 5 -> null
slow at dummy, fast at 2
Why: To create a gap of n nodes between slow and fast pointers
Step 2: Move slow and fast pointers forward together until fast reaches the end
head -> 1 -> 2 -> 3 -> 4 -> 5 -> null
slow at 3, fast at 5
Why: Slow pointer will be just before the node to remove
Step 3: Remove node after slow (node 4) by changing slow.next to slow.next.next
head -> 1 -> 2 -> 3 -> 5 -> null
Why: This skips the 4th node, effectively removing it
Result:
1 -> 2 -> 3 -> 5 -> null
Annotated Code
DSA Python
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def remove_nth_from_end(head: ListNode, n: int) -> ListNode:
    dummy = ListNode(0, head)  # dummy node to handle edge cases
    slow = dummy
    fast = dummy

    # Move fast pointer n steps ahead
    for _ in range(n):
        fast = fast.next

    # Move both pointers until fast reaches the end
    while fast.next:
        slow = slow.next
        fast = fast.next

    # Remove the nth node from end
    slow.next = slow.next.next

    return dummy.next

def print_list(head: ListNode):
    curr = head
    result = []
    while curr:
        result.append(str(curr.val))
        curr = curr.next
    print(" -> ".join(result) + " -> null")

# Driver code
head = ListNode(1, ListNode(2, ListNode(3, ListNode(4, ListNode(5)))))
n = 2
new_head = remove_nth_from_end(head, n)
print_list(new_head)
dummy = ListNode(0, head) # dummy node to handle edge cases
Create dummy node to simplify removal especially if head is removed
for _ in range(n): fast = fast.next
Advance fast pointer n steps ahead to create gap
while fast.next: slow = slow.next fast = fast.next
Move slow and fast together until fast reaches last node
slow.next = slow.next.next
Skip the target node by linking slow.next to node after next
return dummy.next
Return head of modified list, skipping dummy
OutputSuccess
1 -> 2 -> 3 -> 5 -> null
Complexity Analysis
Time: O(n) because we traverse the list twice with two pointers but each node is visited at most once
Space: O(1) because we only use a few pointers and no extra data structures
vs Alternative: Naive approach would find list length first then remove node, requiring two passes; this uses one pass with two pointers for efficiency
Edge Cases
Removing the head node (n equals list length)
Dummy node allows removal without special case; head is removed correctly
DSA Python
dummy = ListNode(0, head)  # dummy node to handle edge cases
List with only one node and n=1
Returns empty list (null) after removal
DSA Python
dummy = ListNode(0, head)  # dummy node to handle edge cases
n is 1 (remove last node)
Last node is removed by slow pointer stopping at second last node
DSA Python
while fast.next:
    slow = slow.next
    fast = fast.next
When to Use This Pattern
When asked to remove the nth node from the end of a linked list, use two pointers spaced n apart to find the target node in one pass.
Common Mistakes
Mistake: Not using a dummy node and failing when removing the head node
Fix: Add a dummy node before head to simplify removal logic
Mistake: Moving fast pointer n+1 steps instead of n, causing off-by-one errors
Fix: Move fast pointer exactly n steps ahead before moving both pointers
Summary
Removes the nth node from the end of a singly linked list in one pass.
Use when you need efficient removal without counting total nodes first.
The key is to keep two pointers n nodes apart so the slow pointer reaches just before the target node.

Practice

(1/5)
1. What does the problem Remove Nth Node from End of List ask you to do?
easy
A. Add a new node at the Nth position from the start
B. Find the value of the Nth node from the start
C. Reverse the linked list up to the Nth node
D. Delete the node that is the Nth from the end in a linked list

Solution

  1. Step 1: Understand the problem statement

    The problem requires removing a node counting from the end of the linked list, not from the start.
  2. Step 2: Identify the correct action

    Deleting the Nth node from the end means locating it and removing it from the list.
  3. Final Answer:

    Delete the node that is the Nth from the end in a linked list -> Option D
  4. Quick Check:

    Remove Nth node from end = Delete node Nth from end [OK]
Hint: Focus on 'from end' means counting backwards [OK]
Common Mistakes:
  • Confusing Nth from start with Nth from end
  • Thinking the problem is about reversing the list
  • Trying to add nodes instead of removing
2. Which of the following is the correct Python syntax to create a dummy node pointing to the head of a linked list named head?
easy
A. dummy = head.next
B. dummy = ListNode(head)
C. dummy = ListNode(0); dummy.next = head
D. dummy = ListNode(); dummy = head

Solution

  1. Step 1: Understand dummy node creation

    A dummy node is usually created with a value (often 0) and its next pointer set to the head.
  2. Step 2: Check each option

    dummy = ListNode(0); dummy.next = head correctly creates a dummy node with value 0 and points its next to head.
  3. Final Answer:

    dummy = ListNode(0); dummy.next = head -> Option C
  4. Quick Check:

    Dummy node points to head = dummy.next = head [OK]
Hint: Dummy node value 0 and next points to head [OK]
Common Mistakes:
  • Passing head as value instead of next pointer
  • Overwriting dummy after creation
  • Not linking dummy to head
3. What will be the output linked list after removing the 2nd node from the end in the list 1 -> 2 -> 3 -> 4 -> 5 -> null?
medium
A. 1 -> 2 -> 3 -> 5 -> null
B. 2 -> 3 -> 4 -> 5 -> null
C. 1 -> 3 -> 4 -> 5 -> null
D. 1 -> 2 -> 4 -> 5 -> null

Solution

  1. Step 1: Identify the 2nd node from the end

    Counting from the end: 5 (1st), 4 (2nd). So node with value 4 is to be removed.
  2. Step 2: Remove node 4 and link previous node to next

    Node 3's next pointer should now point to node 5, skipping node 4.
  3. Final Answer:

    1 -> 2 -> 3 -> 5 -> null -> Option A
  4. Quick Check:

    Remove 2nd from end (4) = 1->2->3->5 [OK]
Hint: Count nodes from end, remove that node [OK]
Common Mistakes:
  • Removing the wrong node counting from start
  • Not updating the previous node's next pointer
  • Confusing node values
4. Given this code snippet to remove the Nth node from end, what is the error?
def removeNthFromEnd(head, n):
    dummy = ListNode(0)
    dummy.next = head
    first = dummy
    second = dummy
    for i in range(n):
        first = first.next
    while first.next:
        first = first.next
        second = second.next
    second.next = second.next.next
    return dummy.next
medium
A. The for loop should run n+1 times to move first pointer
B. The while loop condition should be while first is not None
C. second.next = second.next.next causes error if second.next is None
D. dummy node is not needed for this problem

Solution

  1. Step 1: Analyze the for loop moving first pointer

    To position first pointer n nodes ahead, it must move n+1 times to maintain gap with second pointer.
  2. Step 2: Understand the impact of moving first pointer only n times

    If first moves only n times, the gap is off by one, causing wrong node removal.
  3. Final Answer:

    The for loop should run n+1 times to move first pointer -> Option A
  4. Quick Check:

    Move first pointer n+1 times for correct gap [OK]
Hint: Move first pointer n+1 times to keep gap [OK]
Common Mistakes:
  • Moving first pointer only n times
  • Incorrect while loop condition
  • Ignoring dummy node usage
5. You have a linked list 1 -> 2 -> 3 -> 4 -> 5 -> null. If you remove the 5th node from the end, what will be the resulting list?
hard
A. 1 -> 3 -> 4 -> 5 -> null
B. 2 -> 3 -> 4 -> 5 -> null
C. 1 -> 2 -> 3 -> 4 -> null
D. 5 -> null

Solution

  1. Step 1: Identify the 5th node from the end

    The list length is 5, so the 5th node from the end is the first node with value 1.
  2. Step 2: Remove the first node and link dummy to second node

    Removing node 1 means the new head is node 2, so list becomes 2 -> 3 -> 4 -> 5 -> null.
  3. Final Answer:

    2 -> 3 -> 4 -> 5 -> null -> Option B
  4. Quick Check:

    Remove 5th from end (head) = 2->3->4->5 [OK]
Hint: Removing Nth from end where N equals list length removes head [OK]
Common Mistakes:
  • Removing last node instead of first
  • Not updating head pointer after removal
  • Confusing node positions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard