Java 2ms, beats 77%, Reverse and merge


  • 0
    V
    public void reorderList(ListNode head) 
        {
            if(head == null || head.next == null || head.next.next == null)
                return;
            ListNode t1 = head, t2 = t1, prev = null, curr, temp;
            while(t2 != null && t2.next != null)
            {
                prev = t1;
                t1 = t1.next;
                t2 = t2.next.next;
            }
            curr = t1.next;
            t1.next = null;
            while(curr != null)
            {
                temp = curr.next;
                curr.next = t1;
                t1 = curr;
                curr = temp;
            }
            t2 = t1;
            t1 = head;
            prev = prev.next = t2;
            while(t1.next != prev)
            {
                curr = t2.next;
                t2.next = t1.next;
                temp = t1.next;
                t1.next = t2;
                t1 = temp;
                t2 = curr;
            }
            t1.next = t2;
            
        }

Log in to reply
 

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