-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
39 lines (34 loc) · 866 Bytes
/
Solution.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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode oddEvenList(ListNode head) {
if (head == null) return null;
int length = 0;
ListNode tail = head,
node = head;
while (node != null) {
length++;
tail = node;
node = node.next;
}
ListNode pre = head;
int i = 1;
node = pre.next;
while (i < length && node.next != null) {
pre.next = node.next;
node.next = null;
tail.next = node;
tail = tail.next;
pre = pre.next;
node = pre.next;
i += 2;
}
return head;
}
}