A social app loads posts from two sources, each a linked list of post timestamps sorted from oldest to newest. The timeline should show all of them as one list, oldest first.
Write mergeFeeds(a, b) that relinks the nodes of both lists into one sorted list and returns its head. When two timestamps are equal, the node from a comes first.
Input: a = [1,2,4], b = [1,3,4] Output: [1,1,2,3,4,4]
Taking the smaller front post each time gives 1, 1, 2, 3, 4, 4.
Input: a = [], b = [0] Output: [0]
a is empty, so the answer is just b.
0 to 50 nodes.-100 <= node.val <= 100Lists are written as arrays here, like [1, 2, 3]. Your function works with ListNode objects, each with val and next.
Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #244 Combine Two Sorted Feeds. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.