Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Simple and Explained solution in Clear category for Common Tail by Selindian
def lists_intersect(a: list[int], b: list[int]) -> int | None:
intersect = None # Initialize with None
while a and b: # Loop while a and b not empty
x, y = a.pop(), b.pop() # ... cut of their last elements as x,y
if x == y: # ... if they are equal
intersect = x # ... ... intersect will become that element
else: # ... else
break # ... ... break the loop
return intersect # return the intersect
print("Example:")
print(lists_intersect([1, 2, 3, 4], [5, 6, 3, 4]))
assert lists_intersect([], [1, 2, 3]) == None
assert lists_intersect([1], [1]) == 1
assert lists_intersect([3], [1, 2, 3]) == 3
print("The mission is done! Click 'Check Solution' to earn rewards!")
Sept. 8, 2022