Problems/Linked Lists/Remove the Kth Last Node From a Linked List
Linked Lists
medium

Remove the Kth Last Node From a Linked List

Given a singly linked list, remove the nth node from the end of the list. This problem tests your understanding of linked list manipulation and pointer techniques, crucial for many data structure and algorithm questions.

linked-listtwo-pointersdata-structuresalgorithms

Problem Statement

You are given the head of a singly linked list and an integer 'n'. Your task is to remove the nth node from the end of the linked list and return the head of the modified list. Note that 'n' is guaranteed to be within the bounds of the list's length.

Example 1
Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
Removing the 2nd node from the end (which is 4) results in the list [1,2,3,5].
Example 2
Input: head = [7,9,11,13,15], n = 1
Output: [7,9,11,13]
Removing the 1st node from the end (which is 15) results in the list [7,9,11,13].
Constraints
  • -The linked list will have at least one node.
  • -The value of 'n' will always be a valid positive integer and less than or equal to the length of the linked list.
  • -Node values will be integers.
  • -The linked list is singly linked.

Brute Force Approach

The brute force approach is like trying to find a specific book on a shelf without knowing its position. First, you'd count all the books to know the total number. Then, you'd calculate the position of the book from the beginning based on 'n', and traverse again to reach the book before it to remove it. This involves two traversals of the list.

TimeO(n)
SpaceO(1)

Ready to practice?

Work through this problem with AI coaching and get real-time feedback

Practice This Problem