← 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

Hiring now

ServiceNow has 794 open roles right now

landed applies to roles like these for you, on each company’s own site, with answers written for the role.

Get 5x interviews10 free auto-applies. No card.
  • Customer Success Executive

    Remote

  • CRM Senior Solution Sales Executive

    Brasilia, Brasilia, Brazil

  • CPQ Senior Solution Sales Executive

    Sao Paulo, SAO PAULO, Brazil

Or practice this question first

Next question