Why my result is wrong?


  • 0
    L

    I have output resulthead's value, and it is right.why when I invoke the method, the values of head did not change?please help me.

    public void reorderList(ListNode head) {
    		if(head == null || head.next == null)
    		{
    			return;
    		}
    			ListNode reverse = new ListNode(head.val);
    			ListNode result = new ListNode(0);
    			ListNode resulthead = result;
    			ListNode list = head;
    			while(list.next != null)
    			{
    				list = list.next;
    				ListNode t = new ListNode(list.val);
    				t.next = reverse;
    				reverse = t;
    			}
    			ListNode fast = head;
    			ListNode slow = head;
    			ListNode secondslow = reverse;
    			head = resulthead;
    			while(fast != null && fast.next != null)
    			{
    				ListNode t1 = new ListNode(slow.val);
    				ListNode t2 = new ListNode(secondslow.val);
    				t1.next = t2;
    				result.next = t1;
    				fast = fast.next.next;
    				slow = slow.next;
    				secondslow = secondslow.next;
    				result = result.next.next;
    			}
    			if(fast == null)
    			{
    				result.next = null;
    			}
    			else if(fast.next == null)
    			{
    				result.next = new ListNode(slow.val);
    			}
    
    			head = resulthead.next;
    			
    		
    		
    	}

Log in to reply
 

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