-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeList.java
More file actions
44 lines (42 loc) · 1.45 KB
/
MergeList.java
File metadata and controls
44 lines (42 loc) · 1.45 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
//https://leetcode.com/problems/merge-two-sorted-lists/description/
public class MergeList {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode node1 = list1;
ListNode node2 = list2;
if (list1 == null && list2 == null)
return null;
else if (list1 == null)
return list2;
else if (list2 == null)
return list1;
ListNode merged = new ListNode();
ListNode current = merged;
while (node1 != null && node2 != null) {
// insert value and move along in the list
if (node1.val <= node2.val) {
current.val = node1.val;
node1 = node1.next;
} else {
current.val = node2.val;
node2 = node2.next;
}
// create a new node at the end of the list
// to update on the next iteration
if (node1 != null && node2 != null){
ListNode next = new ListNode();
current.next = next;
current = next;
}
}
// when we exit the loop either list1 or list2 has
// made it to the end
// if both are at the end
if (node1 == null && node2 == null)
current.next = null;
else if (node1 == null)
current.next = node2;
else if (node2 == null)
current.next = node1;
return merged;
}
}