Skip to main content
CodeOath
← All problems

Problem

Remove Nth Node From End of List

Medium
  • linked-list
  • two-pointers

Delete one node from the linked list head: the n-th one when you count backwards from the tail, where the tail itself is number 1. Return the head of the list that is left.

Lists in this problem are given and returned as a plain array of values, in order.

Example 1
Input
head = [10, 20, 30, 40, 50, 60], n = 3
Output
[10, 20, 30, 50, 60]
Explanation

counting from the end, 60 is first, 50 is second and 40 is third, so 40 is removed.

Example 2
Input
head = [4, 8, 15], n = 3
Output
[8, 15]
Explanation

n equals the length of the list, so the first node, 4, is the one removed.

Constraints:

  • 1 <= n <= L, where L is the number of nodes in the list

Follow-up: try to finish with one walk down the list instead of counting the length first and walking a second time.

Tab indents. Press Esc, then Tab to leave the editor.

Run your code to see every test here. Nothing is submitted or recorded.