You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
846 B

2 years ago
package class34;
public class Problem_0328_OddEvenLinkedList {
// 提交时不要提交这个类
public static class ListNode {
int val;
ListNode next;
}
public ListNode oddEvenList(ListNode head) {
ListNode firstOdd = null;
ListNode firstEven = null;
ListNode odd = null;
ListNode even = null;
ListNode next = null;
int count = 1;
while (head != null) {
next = head.next;
head.next = null;
if ((count & 1) == 1) {
firstOdd = firstOdd == null ? head : firstOdd;
if (odd != null) {
odd.next = head;
}
odd = head;
} else {
firstEven = firstEven == null ? head : firstEven;
if (even != null) {
even.next = head;
}
even = head;
}
count++;
head = next;
}
if (odd != null) {
odd.next = firstEven;
}
return firstOdd != null ? firstOdd : firstEven;
}
}