← All questions
MediumCoding

Swapping Nodes in a Linked List

Asked atServiceNow
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below
0
Stuck? Show a way to structure it+
  1. 01Find the kth node from the beginning.
  2. 02From that point, advance a second pointer from head with a trailing pointer.
  3. 03When the lead reaches the end, trailing is kth from the end.
  4. 04Swap values unless the prompt explicitly requires rewiring nodes.

Reference answer

Then expect these follow-ups

  • How would you swap the actual nodes?

    Tests: pointer rewiring

  • Why does the gap method locate kth from end?

    Tests: two-pointer proof

Free to read · better with Enzo

Practice this out loud with Enzo

Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.

Next question