Java Solution


  • 0
    L
      public ListNode oddEvenList(ListNode head) {
            if(head==null) {
                return null;
            } 
            //odd.
            ListNode oddnode = head;
            ListNode oddIt = oddnode;
            ListNode oddPreIt = oddIt;
            
            //even.
            ListNode evennode = head.next;
            ListNode evenIt = evennode;
            while(oddIt!=null && evenIt!=null) {
                oddIt.next = evenIt.next;
                oddPreIt = oddIt;
                oddIt = oddIt.next;
                if(oddIt!=null) {
                    evenIt.next = oddIt.next;
                    evenIt = evenIt.next;
                }
            }
            if(oddIt!=null) {
                oddIt.next = evennode;
            }
            else {
                oddPreIt.next = evennode;
            }
            return oddnode;
        }
    

Log in to reply
 

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