O(1)time,O(1)space.Java


  • 0
    S
    class Solution {
        public ListNode oddEvenList(ListNode head) {
            if(head == null || head.next == null) return head;
            ListNode tmp1 = head;
            ListNode tmp2 = head.next;
            ListNode tmp3 = tmp1.next;
            int i=0;
            while(tmp1.next!=null)
            {
                i++;
                tmp3 = tmp1.next;
                tmp1.next = tmp3.next;
                if(tmp1.next !=null || i%2 == 0)
                    tmp1 = tmp3;
            }
            tmp1.next = tmp2;
            return head;
            
        }
    }

Log in to reply
 

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