160. Intersection of Two Linked Lists
A: a1 → a2
↘
c1 → c2 → c3
↗
B: b1 → b2 → b3# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
if not headA or not headB: return None
a, b = headA, headB
while a != b:
a = a.next if a else headB
b = b.next if b else headA
return aLast updated
Was this helpful?