I am solving a leetcode problem to find the intersection point in a linkedlist. When I use the list it gives me time limit exceed error but when I use set it gives me the answer. As each node in LinkedList is different both set and list would have the same element
Below is my code
def getIntersectionNode(self, headA: ListNode, headB: ListNode):
nodes = set()
while headA != None:
nodes.add(headA)
headA = headA.next
while headB != None:
if headB in nodes:
return headB
headB = headB.next
return None
What is the reason for error to occur when using list instead of set.