Drop the k-th link from the end

Linked lists Mid-level Python 3.13+ Kotlin 2.4+ Reviewed By Best Latest Apps

Predict the output

What does this print?

Python 3.13
vals = [10, 20, 30, 40, 50]
k = 2
lead, trail = k - 1, -1
while lead < len(vals) - 1:
    lead += 1
    trail += 1
print(trail, vals[trail + 1])

Options

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A 2 40

Right: trail stops at index 2, just before 40, the second link from the end.

Index -1 plays the placeholder's role here: it lets trail start before the head, so the head can be removed with the same code.

Why not the others?
  • B 3 50. Trail stays one behind the link being removed.
  • C 1 30. The gap of k links puts trail directly before the target.

30-second answer

Put a placeholder link in front of the head. Move a lead pointer k links ahead, then move lead and trail together until lead is on the last link. Trail now sits just before the link to remove; skip it. The placeholder handles removing the head. One pass: O(n) time, O(1) memory.

Practise saying it · 45 s. No recording, just a timer

Explain removing the k-th link from the end.

Say your answer out loud, then check which of these points you covered:

  • Placeholder head
  • Lead k ahead
  • Skip trail.next

Deep dive

The placeholder head is the template idea here: it gives every real link a predecessor.

Two passes (count, then walk) are also O(n) and perfectly fine if the interviewer allows them.

Keeping a fixed gap between two pointers solves many 'from the end' questions.

Python solution Ran on Python 3.13.5 · mypy --strict
Python 3.13
def drop_from_end(head: Link | None, k: int) -> Link | None:
    front = Link(0, head)  # placeholder in case the head goes
    lead = trail = front
    for _ in range(k):
        lead = lead.next or lead
    while lead.next is not None:
        lead = lead.next
        trail = trail.next or trail
    trail.next = trail.next.next if trail.next else None
    return front.next

Kotlin Ran on Kotlin 2.4.21 · JDK 21.0.12.1
Kotlin 2.4
fun dropFromEnd(head: Link?, k: Int): Link? {
    val front = Link(0, head) // placeholder in case the head goes
    var lead: Link = front
    var trail: Link = front
    repeat(k) { lead = lead.next ?: lead }
    while (lead.next != null) {
        lead = lead.next ?: lead
        trail = trail.next ?: trail
    }
    trail.next = trail.next?.next
    return front.next
}

Common mistakes

  • Forgetting the case where the head itself is removed.
  • An off-by-one gap that removes the neighbour.

Senior take & follow-ups

Name the placeholder trick explicitly; interviewers like to hear why it removes special cases.

Follow-up questions

Think of your answer first, then open the hint.

  1. How do you return the middle link?

    Show hint

    Move one pointer two links per step and another one; once the quicker one runs out, the slower one is halfway.

  2. What if k may exceed the length?

    Show hint

    Check while moving lead; return the head unchanged if lead runs out.

What each operation costs: Linked List · Singly Linked List · Doubly Linked List

From the author

DSA Master - Data & Algorithms

Free on iPhone and iPad: watch 50+ data structures and algorithms step through animations, run the code in the playground, and try the daily challenge.

Suggest a fix if something here is wrong or out of date.