AlgoFrames

Spot the bug: arrows set in the wrong order

Standard · Find the bug · about 8 min

insertAfter(head, after, value) should put a new node holding value right after the first node holding after. It loses part of the list. Click the line with the bug, then fix it.

Example 1

Input:

[[3,8,5,1],5,99]

Output:

[3,8,5,99,1]

The buggy code makes 99 point to itself, so 1 is lost.

Constraints

  • 0 ≤ number of nodes ≤ 5,000
  • If no node holds after, return the list unchanged.

Learn the idea first: Singly linked lists: traverse, insert, delete, a visual lesson.

More Singly linked lists: traverse, insert, delete problems