-
Notifications
You must be signed in to change notification settings - Fork 3
/
Problem-1.java
71 lines (55 loc) · 1.35 KB
/
Problem-1.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
/**
* @author AkashGoyal
*/
/**
--------------------- Problem----------->> Sort a LL of 0's, 1's and 2's
Problem Link :- https://practice.geeksforgeeks.org/problems/given-a-linked-list-of-0s-1s-and-2s-sort-it/1
*/
class Solution
{
//Function to sort a linked list of 0s, 1s and 2s.
static Node segregate(Node head)
{
// add your code here
int zeros=0;
int ones=0;
int twos=0;
Node node=head;
while(head!=null)
{
if(head.data==0)
{
zeros++;
}
else if(head.data==1)
{
ones++;
}
else
{
twos++;
}
head=head.next;
}
Node node1=node;
while(zeros>0)
{
node.data=0;
node=node.next;
zeros--;
}
while(ones>0)
{
node.data=1;
node=node.next;
ones--;
}
while(twos>0)
{
node.data=2;
node=node.next;
twos--;
}
return node1;
}
}