Given head nodes of two linked lists that may or may not intersect, find out if they intersect and return the point of intersection; return null otherwise.

class sll(object):
    def __init__(self,val):
        self.value = val
        self.next = None

    def __repr__(self):
        return str(self.value)
class Solution():
    def find_intersection_point(self,head1,head2):
        length1 = 0
        length2 = 0
        current = head1
        while current:
            current = current.next
            length1+= 1
        current = head2
        while current:
            current = current.next
            length2+=1

        if length1>length2:
            diff = length1-length2
            while diff>0:
                head1 = head1.next
                diff-=1

        elif length2>length1:
            diff = length2-length1
            while diff>0:
                head2 = head2.next
                diff-=1

        while head1 and head2:
            if head1.value == head2.value:
                return head1.value
            head1 = head1.next
            head2 = head2.next

        return None

results matching ""

    No results matching ""