Simple Solution - No complications. May be helpful


  • 0
    N
    public class Solution {
        public ListNode oddEvenList(ListNode head) {
            if(head==null || head.next==null || head.next.next==null)return head;
            
            ListNode oddList = head;
            ListNode evenList = head.next;
            ListNode evenSet = evenList;
            
            while(oddList.next!=null && evenList.next!=null){
                
                oddList.next = oddList.next.next;
                evenList.next = evenList.next.next;
                oddList = oddList.next;
                evenList = evenList.next;
            }
            
            oddList.next = evenSet;
            
            return head;
            
        }
    }

Log in to reply
 

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