Delete the nth node from the end of a singly linked list

Quick Overview

Given the head of a singly linked list and an integer n, delete the nth node counted from the end and return the new head. Tests pointer handling in linked lists, the fast and slow two-pointer technique, and edge cases such as removing the head node.

Delete the nth node from the end of a singly linked list

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given the head of a singly linked list and an integer `n`, delete the nth node from the end of the list and return the modified list's head. The last node is the 1st node from the end. ### Function Signature ```python # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next def remove_nth_from_end(head: Optional[ListNode], n: int) -> Optional[ListNode]: ``` ### Rules - In the examples, a linked list is written as the list of its node values from head to tail. - An empty list (written `[]`) is returned as `None`. This happens when the only node is removed. ### Constraints - `1 <= number of nodes in the list <= 50` - `0 <= Node.val <= 50` - `1 <= n <= number of nodes in the list` ### Examples ```text Input: head = [3, 6, 9, 12, 15], n = 2 Output: [3, 6, 9, 15] ``` Counting from the end, 15 is the 1st node and 12 is the 2nd, so 12 is removed. In a list of 5 nodes that is position 4 from the front. ```text Input: head = [5], n = 1 Output: [] ``` The only node is the 1st from the end. Removing it leaves an empty list. ```text Input: head = [7, 14], n = 1 Output: [7] ``` The last node, 14, is removed, and 7 stays as the head.

Overview: Given the head of a singly linked list and an integer n, delete the nth node counted from the end and return the new head. Tests pointer handling in linked lists, the fast and slow two-pointer technique, and edge cases such as removing the head node.

Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given the head of a singly linked list and an integer n, delete the nth node from the end of the list and return the modified list's head. The last node is the 1st node from the end.

Function Signature

# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next

def remove_nth_from_end(head: Optional[ListNode], n: int) -> Optional[ListNode]:

Rules

  • In the examples, a linked list is written as the list of its node values from head to tail.
  • An empty list (written [] ) is returned as None . This happens when the only node is removed.

Constraints

  • 1 <= number of nodes in the list <= 50
  • 0 <= Node.val <= 50
  • 1 <= n <= number of nodes in the list

Examples

Input: head = [3, 6, 9, 12, 15], n = 2
Output: [3, 6, 9, 15]

Counting from the end, 15 is the 1st node and 12 is the 2nd, so 12 is removed. In a list of 5 nodes that is position 4 from the front.

Input: head = [5], n = 1
Output: []

The only node is the 1st from the end. Removing it leaves an empty list.

Input: head = [7, 14], n = 1
Output: [7]

The last node, 14, is removed, and 7 stays as the head.