Search⌘ K
AI Features

Remove Nth Node from End of List

Explore how to remove the nth node from the end of a singly linked list using the two pointers approach. This lesson helps you understand and implement an efficient single-pass solution, apply linear data structure manipulation, and practice coding in a hands-on environment.

Statement

Given the head of a singly linked list, remove the nthn^{th} node from the end of the list and return the head of the modified list.

Note: Could you solve this in a single pass through the list?

Constraints:

  • The number of nodes in the list is sz

  • 11 \leq sz 30\leq 30

  • 00 \leq Node.val 100\leq 100

  • 11 \leq n \leq szk\leq k

Examples

canvasAnimation-image
1 / 6

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Remove Nth Node From End of List

1.

Given the linked list 42 → 88 → 15 and n = 2, what is the resulting linked list after removing the nth node from the end?

A.

[42, 15]

B.

[42, 88]

C.

[88, 15]

D.

[42, 88, 15]


1 / 5

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5

Try it yourself

Implement your solution in the following coding playground.

Need a nudge?

Explore these hints—each one is designed to guide you a step closer to the solution.

Python
usercode > Solution.py
# Definition for a Linked List node
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
from ds_v1.LinkedList.LinkedList import ListNode
def remove_nth_last_node(head, n):
# Replace this placeholder return statement with your code
return None
Remove Nth Node from End of List