-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path01.05.2024.cpp
75 lines (68 loc) · 1.59 KB
/
01.05.2024.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
Structure of the node of the linked list is as
struct Node
{
char data;
struct Node *next;
Node(int x){
data = x;
next = NULL;
}
};
*/
class Solution
{
public:
// task is to complete this function
// function should return head to the list after making
// necessary arrangements
struct Node* arrangeCV(Node *head)
{
//Code here
if(!head || !head->next) return head;
unordered_set<char> st={'a','e','i','o','u'};
Node*temp=head, *tail;
int cons=0, length=0;
while(temp){
if(st.find(temp->data)==st.end()){
cons++;
}
length++;
if(temp->next==NULL){
tail=temp;
}
temp=temp->next;
}
if(cons==0 || length==cons){
return head;
}
while(head && st.find(head->data)==st.end()){
Node* x=head;
head=head->next;
tail->next=x;
tail=x;
x->next=NULL;
cons--;
}
temp=head;
Node*prev=NULL;
while(cons){
if(tail==temp){
break;
}
if(st.find(temp->data)==st.end()){
prev->next=temp->next;
tail->next=temp;
temp->next=NULL;
tail=temp;
cons--;
temp=prev->next;
}
else{
prev=temp;
temp=temp->next;
}
}
return head;
}
};