用到了两个dummy node。
1 public class Solution { 2 public ListNode oddEvenList(ListNode head) { 3 // Write your solution here 4 if (head == null || head.next == null) { 5 return head; 6 } 7 ListNode odd = new ListNode(0); 8 ListNode oddTail = odd; 9 ListNode even = new ListNode(0); 10 ListNode evenTail = even; 11 ListNode cur = head; 12 while (cur != null) { 13 if (cur.value % 2 == 1) { 14 oddTail.next = cur; 15 oddTail = oddTail.next; 16 } else { 17 evenTail.next = cur; 18 evenTail = evenTail.next; 19 } 20 cur = cur.next; 21 } 22 oddTail.next = even.next; 23 evenTail.next = null; 24 return odd.next; 25 } 26 }