Very Simple Java Solution (1ms)


  • 0
    K
    public ListNode oddEvenList(ListNode head) {
        if(head==null || head.next==null)
        {
            return head;
        }
        //Using p1 and p2, we create two separate LinkList and at the end, join the tail of 
        //odd list to the head of even list.
        ListNode p1=head;
        ListNode p2=head.next;
        ListNode dummy=p2;
        while(p2!=null && p2.next!=null)
        {
            p1.next=p1.next.next;
            p2.next=p2.next.next;
            p1=p1.next;
            p2=p2.next;
        }
        p1.next=dummy;
        return head;
    }

Log in to reply
 

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