-
Notifications
You must be signed in to change notification settings - Fork 1
/
arrange.java
49 lines (41 loc) · 1.59 KB
/
arrange.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
class Solution {
public Node arrangeCV(Node head){
if (head == null || head.next == null)
return head;
Node vowelHead = null, vowelTail = null;
Node consonantHead = null, consonantTail = null;
Node current = head;
while (current != null) {
if (isVowel(current.data)) {
if (vowelHead == null) {
vowelHead = current;
vowelTail = current;
} else {
vowelTail.next = current;
vowelTail = vowelTail.next;
}
} else {
if (consonantHead == null) {
consonantHead = current;
consonantTail = current;
} else {
consonantTail.next = current;
consonantTail = consonantTail.next;
}
}
current = current.next;
}
// Connect the last node of vowel list to the first node of consonant list
if (vowelTail != null)
vowelTail.next = consonantHead;
// Ensure that the last node of the consonant list points to null
if (consonantTail != null)
consonantTail.next = null;
// Return the head of the rearranged list
return (vowelHead != null) ? vowelHead : consonantHead;
}
// Function to check if a character is a vowel
private boolean isVowel(char ch) {
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
}
}