Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Merge Two Sorted Linked Lists

Medium Acceptance 59.00% Points 30.00

You are given the heads of two singly linked lists sorted in non-decreasing order. Your task is to merge them into a single sorted linked list and return the head of the merged list.

Examples
Example 1

Example 1

Input: list1 = [1,2,4], list2 = [1,3,4]

Output: [1,1,2,3,4,4]

Explanation: The two sorted linked lists are merged into a single sorted linked list.

Example 2

Example 2

Input: list1 = [], list2 = [0]

Output: [0]

Explanation: Since the first linked list is empty, the merged list is the second linked list.

Example 3

Example 3

Input: list1 = [2,5,7], list2 = [1,3,6,8]

Output: [1,2,3,5,6,7,8]

Explanation: The nodes from both linked lists are merged while maintaining sorted order.

Hints
Hint 1
N/A
Constraints
  • 0 = Number of nodes in each list = 105
  • -10? = Node.data = 10?
Companies
Amazon Microsoft Apple Samsung
Topics
Link 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