Javascript Solution. Edge cases were a headache on this one for sure. (61.90%)


  • 0
    D
    var oddEvenList = function(head) {
        
        if (!head || !head.next || !head.next.next) return head;
        
        if (!head.next.next.next) {
            head.next.next.next = head.next; // 3 -> 2 -> 3
            head.next = head.next.next; // 1 -> 3 -> 2 -> 3
            head.next.next.next = null; // 1 -> 3 -> 2
            return head;
        }
        
        var odd = head,
            even = head.next,
            first = even;
        
        while (even && even.next) {
            
            odd.next = even.next;
            even.next = even.next.next;
            even = even.next;
            odd = odd.next
            
        }
        
        odd.next = first;
        
        return head;
        
    };
    

Log in to reply
 

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