Data Structures19 sections · 729 units
Open in Course

Problem - Merge Two Sorted Lists

Combine two sorted lists

Given the heads of two sorted linked lists, merge them into one sorted list by splicing together the nodes.

Example: 1241 \to 2 \to 4 and 1341 \to 3 \to 4 merge to 1123441 \to 1 \to 2 \to 3 \to 4 \to 4.

You're implementing the merge step from merge sort, but with linked lists instead of arrays. The linked list version is simpler because you don't need extra space.

Just repoint the next pointers. Constraints: up to 5050 nodes per list, values from 100-100 to 100100, both lists sorted in non-decreasing order.