Linked lists · Problem 2 of 3

Merge two sorted waitlists

Pattern: Merge Two Sorted Linked Lists

easyNot solved yet

The problem

Two branches of a clinic keep waitlists as linked lists of sign-up times, each sorted from earliest to latest. Write a function that merges them into one sorted linked list and returns its head. Reuse the existing nodes rather than creating new ones. Either list may be empty.

text
1merge_waitlists([1, 4, 9], [2, 3, 10])  ->  [1, 2, 3, 4, 9, 10]
2merge_waitlists([], [5, 6])             ->  [5, 6]
3merge_waitlists([2, 2], [2])            ->  [2, 2, 2]
Example 1: a, b and the merged list
1
4
9
null
2
3
10
null
1
2
3
4
9
10
null

Write merge_waitlists(a, b) in Python or mergeWaitlists(a, b) in JavaScript.

Runs privately in your browser. Nothing is sent to a server.Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Write your function, then run it against 18 tests.

Breakdown

Stuck? Reveal one step at a time, and try again after each one.

  1. Step 2
  2. Step 3
  3. Step 4

Solution

?

Give it a real try first: you learn far more from a wrong attempt than from reading the answer.