Easy to understand!!!!!!! 1ms JAVA


  • 2
    S
    public ListNode oddEvenList(ListNode head) {
        if(head==null||head.next==null || head.next.next==null) return head;
         ListNode odd=head;
         ListNode even=head.next;
         ListNode second=even;
         ListNode cur=even.next;
         int i=1;
         while(cur!=null  ){
            if(i%2==1){
                odd.next=cur;
                odd=cur;
            } else{
                even.next=cur;
                even=cur;
            }
            cur=cur.next;            
             i+=1;          
         }
         even.next=null;
         odd.next=second;
         return head;   
    }

Log in to reply
 

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