Java : Intuitive and concise solution


  • 0
    N
    public class Solution {
        public ListNode oddEvenList(ListNode head) {
            if (head == null || head.next == null || head.next.next == null) return head;
            
            ListNode evenhead = head.next;
            ListNode odd = head;
            ListNode even = head.next;;
            
            while(odd.next != null && even.next !=null){
                 if ( even.next != null){
                     odd.next = odd = even.next;
                     
                    if (odd.next != null) {
                     even.next = even = odd.next;
                    }
                 } 
                
            }
            
            // Join the two list and return
            odd.next = evenhead;
            even.next = null;
            return head;
            
        }
    }
    

Log in to reply
 

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