Simple Java solution(1 ms)


  • 0
    R
    public class Solution {
        public ListNode oddEvenList(ListNode head) {
            if(head == null || head.next == null || head.next.next == null) return head;
            
            ListNode odd = head;
            ListNode even = head.next;
            ListNode preserveEven = even;
            ListNode ptr = head.next.next;
            while(ptr != null) {
                odd.next = ptr;
                odd = ptr;
                even.next = ptr.next;
                even = ptr.next;
                ptr = ptr.next == null ? null : ptr.next.next;
            }
            odd.next = preserveEven;
            return head;
        }
    

Log in to reply
 

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