-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegregateEvenOdd.java
More file actions
42 lines (37 loc) · 1.01 KB
/
SegregateEvenOdd.java
File metadata and controls
42 lines (37 loc) · 1.01 KB
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
package LinkedLists;
public class SegregateEvenOdd {
public static Node divide(int N, Node head) {
Node even = null;
Node odd = null;
Node tempE = null;
Node tempO = null;
while (head != null) {
if (head.data % 2 == 0) {
if (even == null) {
even = head;
tempE = head;
} else {
even.next = head;
even = even.next;
}
} else {
if (odd == null) {
odd = head;
tempO = head;
} else {
odd.next = head;
odd = odd.next;
}
}
head = head.next;
}
if (tempE == null) {
if (odd != null) odd.next = null;
return tempO;
} else {
even.next = tempO;
if (odd != null) odd.next = null;
return tempE;
}
}
}