Merge Two Sorted Linked Lists
Updated
Problem statement
Given the heads of two linked lists whose values are sorted in nondecreasing order, splice their existing nodes into one sorted list and return its head.
Duplicate values must be preserved. Either input list may be empty.
Function
mergeTwoLists(listA: ListNode, listB: ListNode) → ListNodeExamples
Example 1
listA = [1,2,4]listB = [1,3,4]return = [1,1,2,3,4,4]From listAFrom listB
Taking the smaller current head yields the merged ordering.
Example 2
listA = []listB = []return = []Two empty lists produce an empty list.
Example 3
listA = []listB = [0]return = [0]The nonempty list is returned.
Constraints
- The two lists contain at most
50000nodes in total. - Node values fit in a signed 32-bit integer.
More ZS Onsite Interview questions
- Employees Whose Manager Left the CompanyONSITE INTERVIEW · Seen Sep 2026
- Merge k Sorted ListsONSITE INTERVIEW · Seen Sep 2026
- Fifth Highest Salary In A DepartmentONSITE INTERVIEW · Seen Sep 2026
- Find Duplicate Email AddressesONSITE INTERVIEW · Seen Sep 2026
- Top Salaries By DepartmentONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Detect a Loop in a Linked ListEPAM · ONSITE INTERVIEWEPAM 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
- Merge Two Sorted Linked ListsApple · ONSITE INTERVIEWApple interview questions
- 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