-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSortedLists.java
70 lines (52 loc) · 1.61 KB
/
MergeSortedLists.java
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
69
70
//LeetCode Problem #21
//Merging 2 sorted singly-linked lists
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public class MergeSortedLists {
public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if ((l1 == null) && (l2 == null)){ return null; }
else {
if(l1 == null){ return l2; }
if(l2 == null){ return l1; }
}
boolean comp = (l1.val <= l2.val);
ListNode currentNode = comp ? l1: l2;
ListNode otherNode = (currentNode == l1) ? l2: l1;
ListNode resNode = currentNode;
ListNode prev = currentNode;
currentNode = currentNode.next;
while(currentNode != null){
if (currentNode.val <= otherNode.val ){
prev = currentNode;
currentNode = currentNode.next;
} else {
prev.next = otherNode;
otherNode = currentNode;
currentNode = prev.next.next;
prev = prev.next;
}
}
prev.next = otherNode;
return resNode;
}
public static void main(String[] args){
ListNode a1 = new ListNode(1);
a1.next = new ListNode(3);
a1.next.next = new ListNode(9);
a1.next.next.next = new ListNode(10);
ListNode a2 = new ListNode(2);
a2.next = new ListNode(4);
a2.next.next = new ListNode(5);
a2.next.next.next = new ListNode(6);
ListNode merged = mergeTwoLists(a1, a2);
System.out.println(merged.val);
ListNode current = merged.next;
while(current != null){
System.out.println(current.val);
current = current.next;
}
}
}