Cancel the K-th Last Booking

Easy · Linked Lists · about 15 min

A booking system keeps its reservations in a linked list, oldest first. A customer asks to cancel the reservation that is k-th from the end (k = 1 is the newest one, the last node).

Build the chain from values, unlink that one node, and return the remaining values in order. Try to do it in a single pass with two pointers.

  • 1 <= k <= len(values), so the list is never empty.
  • Values can repeat: remove the node at that position, not every node with that value.

Example: cancel_from_end([10, 20, 30, 40, 50], 2) → [10, 20, 30, 50]

Hints

  • If one pointer is k nodes ahead of the other, the gap between them stays k as they move together.
  • Start both pointers on a dummy node placed before the head, so there is always a node before the one you remove.
  • Stop when the front pointer is on the last node. The back pointer's next node is the one to unlink.

Learn the pattern first: the Linked Lists lesson.

More Linked Lists problems