-
Notifications
You must be signed in to change notification settings - Fork 1
/
Merge_2_Lists.java
98 lines (96 loc) · 2.42 KB
/
Merge_2_Lists.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
package Programs2;
import java.util.Scanner;
class LinkedList<T> {
T data;
LinkedList<T> next;
LinkedList(T d) {
data = d;
}
}
public class Merge_2_Lists {
static LinkedList createLinkedList() {
Scanner s = new Scanner(System.in);
System.out.print("Enter data : ");
int data = s.nextInt();
LinkedList head = null;
LinkedList temp=null;
while (data != -1) {
LinkedList node = new LinkedList(data);
if (head == null) {
head = node;
temp=head;
} else {
temp.next=node;
temp=temp.next;
}
System.out.print("Enter next data : ");
data = s.nextInt();
}
return head;
}
static void display(LinkedList head)
{
LinkedList temp=head;
while(temp!=null){
System.out.print(temp.data+" ");
temp= temp.next;
}
}
public static LinkedList<Integer> mergeTwoList(LinkedList<Integer> head1, LinkedList<Integer> head2) {
LinkedList<Integer> temp1=head1,temp2=head2,tail=null,head=null;
if(temp1.data.compareTo(temp2.data)>0)
{
head=temp2;
tail=temp2;
temp2=temp2.next;
}
else
{
head=temp1;
tail=temp1;
temp1=temp1.next;
}
while(temp1!=null&&temp2!=null)
{
if(temp1.data.compareTo(temp2.data)>0)
{
tail.next=temp2;
temp2=temp2.next;
}
else
{
tail.next=temp1;
temp1=temp1.next;
}
tail=tail.next;
}
if(temp1!=null)
{
tail.next=temp1;
}
if(temp2!=null)
{
tail.next=temp2;
}
return head;
}
public static void main(String[] args) {
System.out.println("Enter the elements of First Linked List in increasing order.");
LinkedList head1 = createLinkedList();
Scanner s=new Scanner(System.in);
LinkedList temp1 = head1;
System.out.println();
System.out.println("Enter the elements of Second Linked List in increasing order.");
LinkedList head2 = createLinkedList();
LinkedList temp2 = head2;
System.out.print("First Linked List formed is : ");
display(temp1);
System.out.println();
System.out.print("Second Linked List formed is : ");
display(temp2);
System.out.println();
System.out.print("Linked List formed after Merging both the Linked Lists in increasing order is : ");
LinkedList head3=mergeTwoList(head1, head2);
display(head3);
}
}