Java(1ms) easy to understand


  • 1

    public ListNode oddEvenList(ListNode head) {

        if(head==null || head.next==null) return head;
        ListNode odd=head;
        ListNode even=head.next;
        ListNode evenHead=even; //store the head of even node.
        int i=1; //sign the node is whether odd or even.
        if(even.next==null) return head;
        ListNode cur=even.next;  //from the third node start 
        while(cur!=null){
            if(i%2==1){
                odd.next=cur;
                odd=cur;
            }
            else{
                even.next=cur;
                even=cur;
            }
            i++;
            cur=cur.next;
        }
        even.next=null;
        odd.next=evenHead;
        
        return head;
    }
    

    }


Log in to reply
 

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