-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeTwoSortedListsJavaRecursive.java
More file actions
40 lines (35 loc) · 1.46 KB
/
MergeTwoSortedListsJavaRecursive.java
File metadata and controls
40 lines (35 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* 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 mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) return l2;
if (l2 == null) return l1;
if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else {
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}
}
/*
Approach: To solve this recursively we need to identify base case;
l1 is null (we reached end in l1) -> we try l2
l2 is null (we reached end in l2) -> we try l1
l1.val < l2.val -> we take l1, but before returning it we make recursive call to mergeTwoLists where we move are probing l1.next vs l2 as candidate for l1.next
l1.val not < l2.val -> we take l2, but before returning it we make recursive call to mergeTwoLists where we move are probing l1 vs l2.next as candidate for l2.next
the key here is that in order to avoid stack overflow we need to call mergeTwoLists(l1.next, l2) and mergeTwoLists(l1, l2.next) in mirorr-like way,
when parameters are passed not in the same order.
Time complexity: O(n)
Space complextiy: O(n) call stack size
where n is size of both l1 and l2 combined
*/