Logo
Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Solution: Remove Nth Node From End of List

Problem Statement

Given a linked list, remove the last nth node from the end of the list and return the head of the modified list.

Example 1:

  • Input: list = 1 -> 2 -> 3 -> 4 -> 5, n = 2
  • Expected Output: 1 -> 2 -> 3 -> 5
  • Justification: The 2nd node from the end is "4", so after removing it, the list becomes [1,2,3,5].

Example 2:

  • Input: list = 10 -> 20 -> 30 -> 40, n = 4
  • Expected Output: 20 -> 30 -> 40
  • Justification: The 4th node from the end is "10", so after removing it, the list becomes [20,30,40].

Example 3:

.....

.....

.....

Like the course? Get enrolled and start learning!