I am also getting MLE which doesn't make much sense? =(


  • 0
    Y

    since I only declared 3 pointers?

    public class Solution {
    
    public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    	if (headA==null || headB==null)
    	{
    		return null;
    	}
    
    	ListNode end = headA;
    	while(end.next!=null) end = end.next;
    
    	end.next = headA;
    
    	ListNode ptr1 = headB.next;
    	if (ptr1==null) return null;
    	
    	ListNode ptr2 = headB.next.next;
    
    	while(ptr1!=ptr2) {
    		ptr1 = ptr1.next;
    	    if (ptr1==null) return null;
    	    if (ptr2.next==null) return null;
    		ptr2 = ptr2.next.next;
    	}
    	
    	ptr2 = headB;
    	while(ptr1 != ptr2) {
    		ptr1 = ptr1.next;
    		ptr2 = ptr2.next;
    	}
    
    	end.next = null;
    
        return ptr1;
        
    }
    

    }


  • 2
    S

    As your code currently stands, it should trigger 'Linked structure was modified error.' instead of MLE. As stated in the problem description, you must revert all changed made to the linkage of the lists. It may appear that you have do so by putting 'end.next = null' at the end of the function, but the fact is the function could return before the last statement is reached, and returns with modified lists. Please reconsider your algorithm and make sure the lists are properly restored at the exit of the function.


  • 0
    Y

    You are absolutely right Stellari. It is due to structure modification rather than MLE. After I fix the issue, it is accepted now. Thanks again


Log in to reply
 

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