Ctrl K

Drop the K-th Node From the Tail

Medium
You are given a singly linked list head and a number k. Remove the node that is k-th when counting from the end (the last node is the 1st from the end) and return the head of the updated list. If the removed node was the only one, return an empty list. Try to do it in a single pass over the list.
Example 1
head=list_node([6, 0, 8, 3]), k=3 → [6, 8, 3]

Counting from the end: 3 is first, 8 second, 0 third. That one is removed.

Example 2
head=list_node([42, 17]), k=2 → [17]

The second node from the end is the head, so the list now starts at 17.

Example 3
head=list_node([5]), k=1 → None

The only node is removed and the list becomes empty.

Constraints
  • The list has between 1 and 30 nodes
  • 0 ≤ Node.val ≤ 100
  • 1 ≤ k ≤ length of the list
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMove the Tail to the Front MediumSorted List, Unique Values Only MediumSplit a List Around a Pivot MediumList Folded From Both Ends MediumSorting a Linked List Medium
loading Python…
Input
head=list_node([6, 0, 8, 3]), k=3
Expected
[6, 8, 3]

Run checks the first 3 tests in your browser. Submit checks all 79 tests on the server, hidden ones included.