-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathMergeLists.java
102 lines (92 loc) · 3.09 KB
/
MergeLists.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Write a SortedMerge() function that takes two lists, each of which is sorted in increasing order,
// and merges the two together into one list which is in increasing order.
// SortedMerge() should return the new list. The new list should be made by splicing
// together the nodes of the first two lists.
// For example if the first linked list a is 5->10->15 and the other linked list b is 2->3->20,
// then SortedMerge() should return a pointer to the head node of the merged list 2->3->5->10->15->20.
// There are many cases to deal with: either ‘a’ or ‘b’ may be empty,
// during processing either ‘a’ or ‘b’ may run out first,
// and finally there’s the problem of starting the result list empty,
// and building it up while going through ‘a’ and ‘b’.
public class Node{
public int data;
public Node next;
public Node(int data){
this.data = data;
this.next = null;
}
}
public class LinkedList{
public Node head;
public LinkedList(){
head = null;
}
public void insertNodeAtBegin(int data){
Node node = new Node(data);
node.next = head;
head = node;
}
public void insertNodeAtEnd(int data) {
if (head == null) {
insertNodeAtBegin(data);
} else {
Node n = new Node(data);
Node currNode = head;
while (currNode.next != null) {
currNode = currNode.next;
}
currNode.next = n;
}
}
}
public class MergeLinkedLists{
private LinkedList a;
private LinkedList b;
public MergeLinkedLists(LinkedList a,LinkedList b){
this.a = a;
this.b = b;
}
public LinkedList merge(){
LinkedList result = new LinkedList();
while(a.head != null && b.head != null){
if(a.head.data < b.head.data){
result.insertNodeAtEnd(a.head.data);
a.head = a.head.next;
}else{
result.insertNodeAtEnd(b.head.data);
b.head = b.head.next;
}
}
while(a.head != null){ // b empty?
result.insertNodeAtEnd(a.head.data);
a.head = a.head.next;
}
while (b.head != null) {
result.insertNodeAtEnd(b.head.data);
b.head = b.head.next;
}
return result;
}
public void print(Node head){
while(node != null){
System.out.print(node.data + " -> ");
node = node.next;
}
System.out.println(" ");
}
public static void main(String args[]){
LinkedList listA = new LinkedList();
listA.insertNodeAtBegin(15);
listA.insertNodeAtBegin(10);
listA.insertNodeAtBegin(5);
LinkedList listB = new LinkedList();
listB.insertNodeAtBegin(20);
listB.insertNodeAtBegin(3);
listB.insertNodeAtBegin(2);
MergeLinkedLists mergeA = new MergeLinkedLists(listA,listB);
mergeA.print(listA.head);
mergeA.print(listB.head);
LinkedList result = new LinkedList();
mergeA.print(result.head);
}
}