-
Notifications
You must be signed in to change notification settings - Fork 1
/
328 Odd Even Linked List.JAVA
47 lines (46 loc) · 1.25 KB
/
328 Odd Even Linked List.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
class Solution {
public ListNode oddEvenList(ListNode head) {
ListNode evenhead = null;
ListNode eventail = null;
ListNode oddhead = null;
ListNode oddtail = null;
ListNode curr = head;
int check = 1;
while(curr != null){
if(check % 2 == 0){
if(evenhead == null){
evenhead = curr;
eventail = curr;
curr = curr.next;
}
else{
eventail.next = curr;
eventail = eventail.next;
curr = curr.next;
}
}
else{
if(oddhead == null){
oddhead = curr;
oddtail = curr;
curr = curr.next;
}
else{
oddtail.next = curr;
oddtail = oddtail.next;
curr = curr.next;
}
}
check++;
}
if(oddhead == null){
return evenhead;
}
if(evenhead == null){
return oddhead;
}
eventail.next = null;
oddtail.next = evenhead;
return oddhead;
}
}