Java 0ms solution,easy to understand


  • 5
    public ListNode oddEvenList(ListNode head) {
    	if(head!=null){
        	ListNode odd = head;
        	ListNode even = head.next;
        	ListNode p = even;
        	
        	while(odd.next!=null && even.next!=null){
        		odd.next = even.next;
        		odd = odd.next;
        		even.next = odd.next;
        		even = even.next;
        	}
        	odd.next = p;
    	}
    	return head;
    }

  • 0
    X
     if(head!=null) {
            ListNode odd = head;
    		ListNode even = odd.next;
    		ListNode evenFirst=even;
    		while (odd.next != null && even.next!=null) {
    			odd=odd.next=even.next;
    			even=even.next=odd.next;
    		}
        odd.next=evenFirst;	
    }
    return head;

Log in to reply
 

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