-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion23.java
More file actions
95 lines (80 loc) · 1.75 KB
/
Copy pathQuestion23.java
File metadata and controls
95 lines (80 loc) · 1.75 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
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
/*
Author: Ananthanarayanan R
Section: Algorithms
Question: 23
*/
/*
TestCases-
*/
public class Question23
{
public static ListNode mergeKLists(ListNode[] lists)
{
//Failure Cases
if(lists == null || lists.length==0)
return null;
ListNode head = null;
ListNode head2 = null;
ListNode temp = null;
int index = 0;
int nullCount = 0;
int min=Integer.MAX_VALUE;
boolean flag = true;
while(nullCount<lists.length)
{
min=Integer.MAX_VALUE;
nullCount = 0;
for(int i = 0;i<lists.length;i++)
{
if(lists[i] == null)
nullCount++;
else
{
if(lists[i].val<min)
{
min = lists[i].val;
temp = lists[i];
index = i;
}
}
}
if(nullCount==lists.length)
break;
else
{
if(flag)
{
head = temp;
head2 = head;
flag = false;
}
else
{
head.next = temp;
head = head.next;
}
lists[index] = lists[index].next;
}
}
return head2;
}
public static void main(String[] args)
{
System.out.println("Main Method starts");
ListNode head1 = LinkedList.createLinkedList(10,"sorted");
LinkedList.displayLinkedList(head1);
ListNode head2 = LinkedList.createLinkedList(2,"sorted");
LinkedList.displayLinkedList(head2);
ListNode head3 = LinkedList.createLinkedList(3,"sorted");
LinkedList.displayLinkedList(head3);
ListNode head4 = LinkedList.createLinkedList(7,"sorted");
LinkedList.displayLinkedList(head4);
ListNode[] array = new ListNode[4];
array[0] = head1;
array[1] = head2;
array[2] = head3;
array[3] = head4;
ListNode result = mergeKLists(array);
LinkedList.displayLinkedList(result);
}
}