Merge Two Sorted Lists
Given the heads of two sorted singly linked lists, splice their nodes together into one sorted list and return its head. The result should be built by reusing the existing nodes.
Open official problem prompt ↗Interleave two sorted lists into one sorted list by relinking existing nodes, using no extra data structures.
Merging two sorted stacks of numbered cards: each turn you compare the top card of each stack and place the smaller onto the output pile.
- Input
- list1 = [1,2,4], list2 = [1,3,4]
- Output
- [1,1,2,3,4,4]
- Why
- Repeatedly taking the smaller current head yields 1,1,2,3,4,4 in sorted order.
The number of nodes in both lists is in the range [0, 50]-100 <= Node.val <= 100Both list1 and list2 are sorted in non-decreasing order