Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Merge Two Sorted Lists

Medium Acceptance 65.70% Points 30.00

Given two sorted linked lists consisting of nodes respectively. The task is to merge both lists and return the head of the merged list.

Examples
Example 1

Example 1

Input: LinkedList1: 10->20->30, LinkedList2: 5->15->25->35

Output: 5->10->15->20->25->30->35

Explanation: Nodes from both linked lists are merged step-by-step into a new sorted linked list.

Example 2

Example 2

Input: LinkedList1: 5->6->7, LinkedList2: 5->6->7

Output: 5->5->6->6->7->7

Explanation: Duplicate elements are allowed and are added in the order of their occurrence.

Hints
Hint 1
Expected Time Complexity: O(n+m)
Hint 2
Expected Auxilliary Space: O(1)
Constraints
  • 1 <= no. of nodes<= 10^5
  • 0 <= node->data <= 10^5
Companies
Amazon Microsoft Samsung Oracle Flipkart + 1 more
Topics
Linked List
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions