-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathSolution.java
More file actions
36 lines (36 loc) · 852 Bytes
/
Solution.java
File metadata and controls
36 lines (36 loc) · 852 Bytes
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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode oddEvenList(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode odd=head;
ListNode even=head.next;
ListNode node=head;
ListNode oddStart=odd;
ListNode evenStart=even;
head=head.next.next;
int i=1;
while (head!=null) {
if (i%2==0) {
even.next=head;
even=even.next;
}else {
odd.next=head;
odd=odd.next;
}
head=head.next;
i++;
}
even.next=null;
odd.next=evenStart;
return oddStart;
}
}