I don't see the reason for MLE, I only defined several variables?


  • 0
    A
    public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) return null;
        int la = 0, lb = 0;
        ListNode a = headA, b = headB;
        // Find the length of both lists
        while(a != null) {
        	la ++; a = a.next;
        }
        while(b != null) {
        	lb ++; b = b.next;
        }
        // Swap to make sure list a is longer
        if (lb < la) {
        	a = headA; b = headB;
        } else {
        	int temp = la; la = lb; lb = temp;
        	a = headB; b = headA;
        }
        // Travel from the head of list a until the remaining length = list b
        while (la-- > lb) a = a.next;
        
        // Travel on until the end or lists merge
        while (a != null && b != null) {
        	if (a == b) return a;
        	a = a.next;
        	b = b.next;
        } return null;
    }
    

    }


  • 0
    S

    I have tried your code and it was accepted. Could you please give it a try again?


Log in to reply
 

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