Java solution with 2ms


  • 0
    S
    public void reorderList(ListNode head) {
        int k=0;
        if(head==null||head.next==null) return;
        ListNode head1,p=head,q;
        while(p!=null){
            p=p.next;
            k++;
        }
        k=(k+1)/2;
        p=head;
        while(--k>0){
            p=p.next;
        }
        q=p.next;
        p.next=null;
        head1=q;
        p=head1;
        q=p.next;
        while(q!=null){
            p.next=q.next;
            q.next=head1;
            head1=q;
            q=p.next;
        }
        while(head1!=null){
            q=head.next;
            p=head1.next;
            head.next=head1;
            head1.next=q;
            head=q;
            head1=p;
        }
    }

Log in to reply
 

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