FastPrepMerge Two Sorted Linked Lists

Merge Two Sorted Linked Lists

Adobe logoAdobe● EasyFULLTIMEPHONE SCREEN

Updated

Problem statement

You are given the heads of two singly linked lists, list1 and list2. Each list is already sorted from smallest to largest value (equal values may repeat).

Join all nodes of both lists into one list that is also sorted from smallest to largest, by changing the links between the existing nodes, and return its head. Keep every node, including repeated values.

Function

mergeTwoSortedLists(list1: ListNode, list2: ListNode) → ListNode

Examples

Example 1

list1 = [1,4,4,9]list2 = [2,4,10]return = [1,2,4,4,4,9,10]
list1
1449
list2
2410
return
12444910
From list1From list2

Take the smaller front value each time: 1, 2, then the three 4s (two from list1, one from list2), then 9 and 10.

Example 2

list1 = []list2 = [-3,0]return = [-3,0]
list1
empty
list2
-30
return
-30

list1 is empty, so the answer is list2.

Example 3

list1 = [5,6,7]list2 = [1,2]return = [1,2,5,6,7]
list1
567
list2
12
return
12567
From list1From list2

Every value in list2 is smaller, so its nodes come first, followed by all of list1.

Constraints

  • Each list has between 0 and 1000 nodes.
  • -1000 <= node.val <= 1000
  • Each list is sorted in non-decreasing order.

More Adobe Phone Screen questions

Similar questions at other companies

Interview reports that mention this question

Adobe interview experiences

All Adobe interview questionsSee Adobe hiring insights
Saved
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeTwoSortedLists(ListNode list1, ListNode list2) {
        // Write your code here.
    }
}
Checking account…