C# solution


  • 0
    public class Solution {
        public ListNode GetIntersectionNode(ListNode headA, ListNode headB) {
    
            
            if(headA == null || headB == null)
            {
                return null;
            }
            
            ListNode a = headA;
            ListNode b = headB;
            
            while(a != null && b !=null && a != b)
            {
                a = a.next;
                b = b.next;
                if(a == b)
                {
                    return a;
                }
                
                if(a == null)
                {
                    a = headB;
                }
                else if(b == null)
                {
                    b = headA;
                }
            }
            
            return a;
        }
    }
    

Log in to reply
 

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