/** * 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 || head.next == null) return head; ListNode odd = head; ListNode even = head.next; int cnt = 1; ListNode curr = even.next; even.next = null; while(curr != null){ ListNode tmp = curr; curr = curr.next; if(cnt == 1){ tmp.next = odd.next; odd.next = tmp; odd = tmp; cnt--; }else { tmp.next = even.next; even.next = tmp; even = tmp; cnt++; } } return head; } }