Write a program to find the node at which the intersection of two singly linked lists begins.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        visited = set()
        while headA or headB:
            if headA:
                if headA in visited:
                    return headA
                else:
                    visited.add(headA)
                headA=headA.next
            if headB:
                if headB in visited:
                    return headB
                else:
                    visited.add(headB)
                headB=headB.next

        return None

Like this post? Share on: TwitterFacebookEmail


Keep Reading


Published

Category

Kieran

Tags