-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrange consonants and vowels
90 lines (70 loc) · 1.78 KB
/
Arrange consonants and vowels
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
//Arrange consonants and vowels
import java.io.*;
import java.util.*;
class Node {
char data;
Node next;
public Node(char data){
this.data = data;
next = null;
}
}
class GFG {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0) {
int n = sc.nextInt();
Node head = null, tail = null;
char head_c = sc.next().charAt(0);
head = new Node(head_c);
tail = head;
while(n-- > 1) {
tail.next = new Node(sc.next().charAt(0));
tail = tail.next;
}
Solution obj = new Solution();
show(obj.arrangeCV(head));
}
}
public static void po(Object o) {
System.out.println(o);
}
public static void show(Node head) {
while(head != null) {
System.out.print(head.data+" ");
head = head.next;
}
System.out.println();
}
}
Structure of node class is:
class Node {
char data;
Node next;
public Node(char data) {
this.data = data;
next = null;
}
}
Solution {
public Node arrangeCV(Node head){
Node vowel = new Node('a'), con = new Node('b');
Node vowelH = vowel, conH = con;
while(head != null) {
char temp = head.data;
if(temp == 'a' || temp == 'e' || temp == 'i' || temp == 'o' || temp == 'u') {
vowel.next = head;
vowel = vowel.next;
}
else {
con.next = head;
con = con.next;
}
head = head.next;
}
con.next = null;
vowel.next = conH.next;
return vowelH.next;
}
}