Quick Overview

Merge two sorted linked lists by relinking nodes with a dummy head, preserving duplicates and handling empty inputs in linear time and constant space.

Merge Two Sorted Linked Lists

Company: LinkedIn

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

Merge two ascending singly linked lists into one ascending list containing all their nodes. Preserve duplicate values. You may relink existing nodes. ### Clarifying Questions to Ask - May the input lists be modified, and are they disjoint and acyclic? - Should equal values from the first list precede equal values from the second? ```hint Keep an output tail Once the smaller head is chosen, it never needs to move again. ``` ### What a Strong Answer Covers - A dummy head or equivalent treatment of the first output node. - Advancing only the selected list and appending the remaining suffix. - Empty inputs, duplicate values, and mutation assumptions. ### Follow-up Questions - How would you merge k sorted lists? - What extra memory is required if input nodes must remain unchanged?

Overview: Merge two sorted linked lists by relinking nodes with a dummy head, preserving duplicates and handling empty inputs in linear time and constant space.

Read the full LinkedIn Machine Learning Engineer interview experience this question came from

Merge two ascending singly linked lists so the result contains every node value in ascending order, including duplicates. In the console, `first` and `second` give the values from head to tail. Return the merged list in the same representation. An empty array represents an empty list.

Constraints

  • The lists are disjoint, acyclic, and nondecreasing, with at most 2^31 − 1 values combined.
  • Values are integers from −(2^53 − 1) through 2^53 − 1, inclusive.

Examples

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

Expected Output: [1, 2, 3, 4, 5, 6]

Explanation: The two input lists interleave.

Input: ([0, 2, 2], [-1, 2, 4])

Expected Output: [-1, 0, 2, 2, 2, 4]

Explanation: All three occurrences of 2 appear in the result.

Hints

  1. Every occurrence of a value belongs in the result, even if it appears in both lists.

Merge Two Sorted Linked Lists

hardLinkedInMachine Learning Engineer

Merge two ascending singly linked lists so the result contains every node value in ascending order, including duplicates. In the console, first and second give the values from head to tail. Return the merged list in the same representation. An empty array represents an empty list.

Examples

Example 1
Input
([1, 3, 5], [2, 4, 6])
Output
[1, 2, 3, 4, 5, 6]
Notes
The two input lists interleave.
Example 2
Input
([0, 2, 2], [-1, 2, 4])
Output
[-1, 0, 2, 2, 2, 4]
Notes
All three occurrences of 2 appear in the result.

Constraints

  • •The lists are disjoint, acyclic, and nondecreasing, with at most 2^31 − 1 values combined.
  • •Values are integers from −(2^53 − 1) through 2^53 − 1, inclusive.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def merge_sorted_linked_lists(first: list[int], second: list[int]) -> list[int]:
Sample call
merge_sorted_linked_lists([1, 3, 5], [2, 4, 6])
Language
Case 1
Input
([1, 3, 5], [2, 4, 6])
Expected
[1, 2, 3, 4, 5, 6]
Case 2
Input
([0, 2, 2], [-1, 2, 4])
Expected
[-1, 0, 2, 2, 2, 4]

Browse More Questions

Show the approach

Approach

Compare the first unconsumed values of the two lists. The smaller one is the smallest remaining value overall, so append it and advance only that list. Once a list is exhausted, append the suffix of the other. Each input occurrence is appended exactly once. For actual linked nodes, a dummy head and an output tail support the same algorithm by relinking nodes in place.

Time complexity:
O(m + n) for input lengths m and n.
Space complexity:
O(1) auxiliary space excluding the serialized result.