Skip to content
BytePatterns

Merge Sorted Chains

EasyLinked Lists#two-pointers#dummy-node~20m

Problem

Two singly linked lists are each already sorted in non-decreasing order. Splice them into one sorted list by relinking the existing nodes rather than creating new ones. Return the head of the merged list, which is empty when both inputs are empty.

Examples

Input:  a = 1 -> 4 -> 6, b = 2 -> 3
Output: 1 -> 2 -> 3 -> 4 -> 6
Input:  a = empty, b = 7
Output: 7
Why:    edge case, one side runs out before the loop starts
Input:  a = empty, b = empty
Output: empty
Why:    edge case, there is nothing to merge

Hints

0 / 3

Stuck on the idea rather than the code? Singly Linked List Basics covers it.