-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrange Consonants and Vowels.cpp
57 lines (52 loc) · 1.12 KB
/
Arrange Consonants and Vowels.cpp
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
// Time complexity - O(Length of the linked list)
// Space complexity- O(1)
class Solution
{
public:
struct Node* arrangeCV(Node *head)
{
Node* vH=NULL;
Node* vT=NULL;
Node* cH=NULL;
Node* cT=NULL;
Node* temp=head;
while(temp)
{
char ch=temp->data;
if(ch=='a' or ch=='e' or ch=='i' or ch=='o' or ch=='u')
{
if(vH==NULL)
{
vH=temp;
vT=temp;
}
else
{
vT->next=temp;
vT=temp;
}
}
else
{
if(cH==NULL)
{
cH=temp;
cT=temp;
}
else
{
cT->next=temp;
cT=temp;
}
}
temp=temp->next;
}
if(vT==NULL)
return cH;
if(cH==NULL)
return vH;
vT->next=cH;
cT->next=NULL;
return vH;
}
};