160. Intersection of Two Linked Lists
[Easy] “Find the node at which the intersection of two singly linked lists begins”
Python3:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
pointer_a = headA
pointer_b = headB
# basically, append list a onto list b and
# vice-versa and traverse both simultaneously
while pointer_a != pointer_b:
if not pointer_a:
pointer_a = headB
else:
pointer_a = pointer_a.next
if not pointer_b:
pointer_b = headA
else:
pointer_b = pointer_b.next
return pointer_a