Skip to content
CodeKruPro

Merge two sorted linked lists

Coding problemEasyAsked in: Amazon, Flipkart, Microsoft, Oracle, Samsung, Zoho

You are given the heads of two linked lists, first and second. Each is sorted from smallest to largest value. Join them into one list that is also sorted, and return its head.

Either list, or both, may be empty. Values may repeat within and across the lists.

Examples

Example 1
Input: first = [1,2,4], second = [1,3,4]
Output: [1,1,2,3,4,4]
Example 2
Input: first = [], second = [0]
Output: [0]
Example 3
Input: first = [], second = []
Output: []

Constraints

  • Each list has between 0 and 50,000 nodes
  • Each value is between -1,000,000,000 and 1,000,000,000
  • Both lists are sorted ascending

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in