-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeTwoSortedLists.java
More file actions
68 lines (63 loc) · 2.42 KB
/
MergeTwoSortedLists.java
File metadata and controls
68 lines (63 loc) · 2.42 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* LeetCode problem 21. Merge Two Sorted Lists: https://leetcode.com/problems/merge-two-sorted-lists/
*/
public class Solution {
/**
* Merges two sorted linked lists into one sorted linked list
* Time Complexity: O(max(N)), where N = length of the first list, and M = length of the second list
* The while loop runs until either of the two lists are empty, after which in a single step the remaining elements
* of the non-empty list is added to the solution linked list. In the worst case, the two lists have the same length
* and alternate in lowest values, so 2*N steps are taken. The constant can be dropped, and hence O(N).
* <p>
* Space Complexity: O(S), where S = the sum of the lenghts of the two given lists
* Since all the values from both of the lists must be stored, the size of the merged sorted list will be equivalent
* to the sum of the lenghts of the two given lists.
*
* @param list1 the first linked list
* @param list2 the second linked list
* @return the sorted and merged linked list
*/
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode solution = new ListNode();
// pointer to solution;
ListNode curr = solution;
while (list1 != null && list2 != null) {
// compare the two values, append the smaller value to solution linked list
if (list1.val <= list2.val) {
curr.next = new ListNode(list1.val);
// advance the pointer on the linked list which was used
list1 = list1.next;
} else {
curr.next = new ListNode(list2.val);
list2 = list2.next;
}
// advance the solution pointer
curr = curr.next;
}
// check if one of the linked lists is longer than the other, append the longer one
if (list1 == null && list2 != null) {
curr.next = list2;
}
if (list1 != null && list2 == null) {
curr.next = list1;
}
// return from next since first elemented inserted at next of starting node
return solution.next;
}
}
/**
* LinkedList class for problem
*/
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;
}
}