CodePark/ problems / Merge Two Sorted Linked Lists
START PRACTICINGPRACTICE
Coding problem · Data structures and algorithms › Linked lists

Merge Two Sorted Linked Lists

easyabout 12 minutes in an interview

Combine two sorted linked lists into one sorted list and return its head.

You get the heads of two singly linked lists, a and b. Each list is sorted in ascending order (smallest first). The result must also be in ascending order.

  • Build the result by reusing the existing nodes. Relink their next references. Don't create new nodes.
  • Either list can be null (empty). If both are null, return null.
  • Values can repeat, inside one list and across both lists. Values can be negative.
Input: a = 1 → 4 → 6, b = 2 → 3 → 7 Output: 1 → 2 → 3 → 4 → 6 → 7
Input: a = null, b = 0 → 5 Output: 0 → 5