Merge Two Sorted Linked Lists
Updated
Problem statement
Inputs: list1 and list2, the heads of two linked lists. Either may be empty, and inside each one the values never decrease from node to node.
Output: the head of one list that uses every node of both inputs, repeated values included, with values that again never decrease. If both inputs are empty, the output is empty.
Function
mergeTwoLists(list1: ListNode, list2: ListNode) → ListNodeExamples
Example 1
list1 = [0,0,5,17]list2 = [2,3,30,31,32]return = [0,0,2,3,5,17,30,31,32]From list1From list2
Both 0s from list1 are smaller than anything in list2, so they lead. 2 and 3 fit before 5, then 5 and 17 come next. list1 is then used up, so 30, 31 and 32 close the list.
Example 2
list1 = [5]list2 = [5]return = [5,5]From list1From list2
Two nodes hold the same value. Both are kept, so the answer has two 5s.
Example 3
list1 = [-3,4]list2 = []return = [-3,4]list2 has no nodes, so nothing needs weaving and list1 comes back as it is.
Constraints
- Neither list has more than
50nodes. -100 <= Node.val <= 100- Within each list, every value is at least as large as the value before it.
More Capital One Onsite Interview questions
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
- Sort ColorsONSITE INTERVIEW · Seen Oct 2026
- Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
- Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
- Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
- Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
- Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
- Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025
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
- 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
Capital One interview experiences
- Capital One Coding OA Questions: 3 Experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences