A clear and 0ms java solution by dummy head node


  • 0
    C
    public ListNode oddEvenList(ListNode head) {
        ListNode oddDummy = new ListNode(0), evenDummy = new ListNode(0);
        ListNode oddRear = oddDummy, evenRear = evenDummy, p = head;
        for(int count = 1; p!=null; p=p.next, count++)
            if(count%2 == 1){
                oddRear.next = p;
                oddRear = p;
            }
            else{
                evenRear.next = p;
                evenRear = p;
            }
        evenRear.next = oddRear.next = null;
        oddRear.next = evenDummy.next;
        return oddDummy.next;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.