Java 0 ms


  • 0
    V
    public ListNode oddEvenList(ListNode head) {
            if(head == null || head.next == null) {
                return head;
            }
            ListNode odd = head;
            ListNode even = head.next;
            ListNode evenStart = head.next;
            while(even.next != null) {
                odd.next = even.next;
                odd = odd.next;
                if(odd.next == null) {
                    even.next = null;
                    break;
                }
                even.next = odd.next;
                even = even.next;
            }
            odd.next = evenStart;
            return head;
        }
    

Log in to reply
 

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