tutorialcup
Merge Two Sorted Lists Leetcode Solutions
Linked lists are quite like arrays in their linear properties. We can merge two sorted arrays to form an overall sorted array. In this problem, we have to merge two sorted linked lists in place to return a new list which contains elements of both lists in a sorted fashion.
Example
L1 = 1 -> 2 -> 9
L2 = 1 -> 3 -> 4
1 1 2 3 4 9
Approach
The simplest way to do it would be to use the two-pointer technique. Create a new empty list. Append the smaller elements among both the pointers and increment the corresponding pointer. This is a good approach but requires the creation of an extra list that consumes space.
The Optimal Approach should consume constant space only in order to do an in-place sorting. We can follow the iterative approach. We already have the nodes stored in both the lists. Create a new list pointer and its subsequent "next pointers" should point to predefined nodes in the memory. In this process, we create no new nodes.
Algorithm(Naive Approach)
- Create a function mergeTwoSortedLists() that takes two list pointers as arguments
- If either of the lists is NULL, return the other one
- Create a temp variable that will point to the smaller node among heads of both lists
- Now, at least, one node is appended to our result, so one head should be incremented
- This creates a subproblem. So, call the same recursive function and append it to temp
- If List1.value < List2.value
- temp = new ListNode(List1.value)
www.tutorialcup.com/leetcode-solutions/merge-two-sorted-l...
Merge Two Sorted Lists Leetcode Solutions
Linked lists are quite like arrays in their linear properties. We can merge two sorted arrays to form an overall sorted array. In this problem, we have to merge two sorted linked lists in place to return a new list which contains elements of both lists in a sorted fashion.
Example
L1 = 1 -> 2 -> 9
L2 = 1 -> 3 -> 4
1 1 2 3 4 9
Approach
The simplest way to do it would be to use the two-pointer technique. Create a new empty list. Append the smaller elements among both the pointers and increment the corresponding pointer. This is a good approach but requires the creation of an extra list that consumes space.
The Optimal Approach should consume constant space only in order to do an in-place sorting. We can follow the iterative approach. We already have the nodes stored in both the lists. Create a new list pointer and its subsequent "next pointers" should point to predefined nodes in the memory. In this process, we create no new nodes.
Algorithm(Naive Approach)
- Create a function mergeTwoSortedLists() that takes two list pointers as arguments
- If either of the lists is NULL, return the other one
- Create a temp variable that will point to the smaller node among heads of both lists
- Now, at least, one node is appended to our result, so one head should be incremented
- This creates a subproblem. So, call the same recursive function and append it to temp
- If List1.value < List2.value
- temp = new ListNode(List1.value)
www.tutorialcup.com/leetcode-solutions/merge-two-sorted-l...