Merge Two Sorted Linked Lists
Updated
Problem statement
Given the heads first and second of two singly linked lists, each sorted from smallest to largest, merge them into one sorted linked list and return its head.
Relink the existing data nodes. Do not copy their values into new data nodes. A temporary dummy node is allowed. Keep every duplicate. When both heads have equal values, take the node from first before the node from second.
The two lists share no nodes and have no cycles. Each list is shown as its node values, and an empty list means a null head.
Function
mergeSortedLists(first: ListNode, second: ListNode) → ListNodeExamples
Example 1
first = [1,2,4]second = [1,3,4]return = [1,1,2,3,4,4]From firstFrom second
All six nodes appear once in sorted order, including both ones and both fours.
Example 2
first = []second = [0]return = [0]The first list is empty, so the answer is the second list, unchanged.
Constraints
- Each list contains between
0and10000nodes. -10^9 <= node.val <= 10^9.
More Apple Onsite Interview questions
- Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- FIFO Mutex Operation TraceONSITE INTERVIEW · Seen Oct 2026
- Implement Hit CounterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Parse Nested HTML into a TreeONSITE INTERVIEW · Seen Oct 2026
- Thread-Safe Concurrent Linked ListONSITE INTERVIEW · Seen Sep 2026
- Palindrome Match PercentageONSITE INTERVIEW · Seen Sep 2026
- Binary Tree Nodes at Distance KONSITE INTERVIEW · Seen Aug 2026
- BST to Sorted Circular Doubly Linked ListONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Detect a Loop in a Linked ListEPAM · ONSITE INTERVIEWEPAM interview questions
- Merge Two Sorted Linked ListsZS · ONSITE INTERVIEWZS interview questions
- Middle Node Of A Linked ListNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Build and Check a Palindrome Linked ListOmnissa · ONSITE INTERVIEWOmnissa interview questions
- Reverse a Singly Linked ListGEP · ONSITE INTERVIEW
- Reverse a Singly Linked ListMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Reverse Linked ListThe Walt Disney Company · OA · ONSITE INTERVIEWThe Walt Disney Company interview questions
- Detect a Linked-List CycleGoogle · ONSITE INTERVIEWGoogle interview questions
- Detect a Cycle in a Linked ListPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
- Reverse a Singly Linked ListBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions