Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Merge Two Sorted Lists

Linked Listsmedium

Two sorted linked lists are given as arrays. Implement `mergeTwoSorted(a, b)` returning a single sorted array by merging them.

Sample tests

Input: mergeTwoSorted([1,3,5], [2,4])
Output: [1,2,3,4,5]
Input: mergeTwoSorted([], [1])
Output: [1]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Forgetting to drain the remaining elements of the longer list.

Learning resources

  • Wikipedia: Data structure
Approach & explanation (try first)

Merging two sorted sequences with two pointers is O(n + m) time.

Loading...
⌘/Ctrl + Enter

Run your code to see results.