*****My Java 1ms and Very easy soultion*****


  • 1
    public class Solution {
        public ListNode oddEvenList(ListNode head) {
            if(head==null||head.next==null){return head;}//boundary control
            ListNode odd=head;                          //pointer->odd:conncect odds
            ListNode EvenFirst=head.next;               //hold the first even
            ListNode even=head.next;                    //pointer->even:connect evens
            while(even.next!=null){                     //Space:O(1);Time:O(n)
                odd.next=odd.next.next;
                odd=odd.next;
                if(odd.next==null){break;}
                even.next=even.next.next;
                even=even.next;
            }
            odd.next=EvenFirst;                         //connect last odd and first even
            even.next=null;                             //last even should be the last node
            return head;
        }
    }

  • 0
    P


    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;

        while(odd.next!=null && even.next!=null){
            if(even.next!=null){
                odd.next= even.next;
                odd = odd.next;
            }
            
            if(odd.next!=null){
            even.next = odd.next;
            even = even.next;
            }
            
        }
        
        //if(odd.next == null){
            odd.next = second;
        //}
        even.next = null;
        
        return head;
        
    }
    

  • 0
    J
    This post is deleted!

Log in to reply
 

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