Redirecting to
NADOS

Merge Two Sorted Linked Lists

Try First, Check Solution later

1. You should first read the question and watch the question video.
2. Think of a solution approach, then try and submit the question on editor tab.
3. We strongly advise you to watch the solution video for prescribed approach.

1. You are given a partially written LinkedList class.
2. You are required to complete the body of mergeTwoSortedLists function. The function is static and is passed two lists which are sorted. The function is expected to return a new sorted list containing elements of both lists. Original lists must stay as they were.
3. Input and Output is managed for you.
Input Format
Input is managed for you
Output Format
Output is managed for you
Question Video
Constraints
1. O(n) time complexity and constant space complexity expected.
Sample Input
5
10 20 30 40 50
10
7 9 12 15 37 43 44 48 52 56
Sample Output
7 9 10 12 15 20 30 37 40 43 44 48 50 52 56 
10 20 30 40 50
7 9 12 15 37 43 44 48 52 56


  • Editorial

    The problem here deals with implementing the mergeTwoSortedLists() function which returns the sorted list by combining the two input lists.

    In this problem, we are given two sorted linked lists and we wish to return the sorted combination of these lists. A point to note here is that if an element in list 1 is smaller than list 2 then it is obvious that it will be the smallest among all the remaining elements in both list 1 and list 2. Keeping this in mind, the approach that we are going to follow here is a simple one, wherein, we are keeping two pointers one for list 1 and the other for list 2 both initially at their respective head node. Now a comparison is made between their elements and the smaller one is selected and added to the resultant list and that pointer is updated. This process keeps on going until any one of the lists becomes empty. The other non-empty list is taken care of by adding it at the end of the resultant list.

    For the implementation, we are using an in-built LinkedList class for creating lists, and for performing operations on the list we are using the class in-built functions.

    Below is the implementation in Java:

    public static LinkedList mergeTwoSortedLists(LinkedList l1, LinkedList l2) {
                                        LinkedList ml = new LinkedList(); 
                                        Node one = l1.head;
                                        Node two = l2.head;
                                        while (one != null && two != null) {
                                        if (one.data < two.data) {
                                            ml.addLast(one.data);
                                            one = one.next;
                                        } else {
                                            ml.addLast(two.data);
                                            two = two.next;
                                        }
                                        } 
                                        while (one != null) {
                                        ml.addLast(one.data);
                                        one = one.next;
                                        } 
                                        while (two != null) {
                                        ml.addLast(two.data);
                                        two = two.next;
                                        } 
                                        return ml;
                                    }

    java false

    Dry Run

    List 1: 30 -> 42 -> 46 -> 51 (List A)

    List 2: 12 -> 40 -> 50 (List B)

    Output List: 12 -> 30 -> 40 -> 42 -> 46 -> 50 -> 51

    Initially, one = A1 node and two = B1 node

    1. First Iteration

    one.data ( = 30)>two.data ( = 12)

    Output List: 12 ->null

    one = A2 node and two = B2 node

    2. Second Iteration

    one.data ( = 30)

    Output List: 12 ->30 ->null

    one = A2 node and two = B2 node

    3. Third Iteration

    one.data ( = 42)>two.data (= 40)

    Output List: 12 -> 30 -> 40 ->null

    one = A2 node and two = B3 node

    4. Fourth Iteration

    one.data ( = 42)

    Output List: 12 -> 30 ->40 -> 42 ->null

    one = A3 node and two = B3 node

    5. Fifth Iteration

    one.data ( = 46)

    Output List: 12 -> 30 -> 40 -> 42 -> 46 ->null

    one = A4 node and two = B3 node

    6. Sixth Iteration

    one.data ( = 51)>two.data ( = 50)

    Output List: 12 -> 30 -> 40 -> 42 -> 46 -> 50 ->null

    one = A4 node and two = null

    As one != null adding remaining list A elements,

    Output List: 12 -> 30 -> 40 -> 42 -> 46 -> 50 ->51 ->null

    Hence verified.

    Time Complexity: O(n)

    The time complexity for the function is linear as the traversal of both the lists are involved. Here we visit each and every element in both the lists which makes our algorithm linear. Here n denotes the combined length of both the lists.

    Space Complexity: O(n)

    The space complexity for the function is linear as we have created an output list with size proportional to the combined length of both lists.

  • Asked in Companies
  • Related Topics






Video Solution

Code Solution

Run
 
Run
Id Name