My attempt at In Place reordering fails for large input.


  • 0
    A

    Hello, can anyone point out some mistake in my code? Thanks

    public class Solution {
            public void reorderList(ListNode head) {
                
                if(head==null || head.next==null || head.next.next==null)
                    return;
                    
                ListNode runner = head;
                ListNode rn = runner.next;
                ListNode currlast=null;
                ListNode temp=head;
                int currlength=0;
                
               currlast=temp;
                
                while(runner!=currlast)
                {
                    temp=head;
                    while(temp.next!=currlast)
                        temp=temp.next;
                        
                    if(temp==runner)
                        break;
                        
                    currlast.next=rn;
                    runner.next=currlast;
                    temp.next=null;
                    
                    currlast=temp;
                    runner=runner.next;
                    rn=runner.next;
                    
                }
            
            return ;
        
        }

Log in to reply
 

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