class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
l = []
cur1 = headA
while cur1 is not None:
l.append(cur1)
cur1 = cur1.next
cur2 = headB
while cur2 is not None:
if cur2 in l:
return cur2
cur2 = cur2.next
return None
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
stackA = ['A']
stackB = ['B']
while headA or headB:
if headA:
stackA.append(headA)
headA = headA.next
if headB:
stackB.append(headB)
headB = headB.next
prev = None
while stackA and stackB:
nodeA = stackA.pop(-1)
nodeB = stackB.pop(-1)
if nodeA != nodeB:
return prev
prev = nodeA