Singly linked lists

Insert B after A and report the resulting node order.

Examples

Example 1

Input
A → C
Expected output
A → B → C

Example 2

Input
A
Expected output
A → B

Hints and solution

Hint 1

The new node points at whatever A used to point at.

Reveal worked answer

Set B’s next reference first, then update A’s. If A pointed to C, the result has all three nodes. If A was last, the new final node B points to no next node.

Back to the lesson