longer_node = ll1.head if ll1_len >= ll2_len else ll2.head shortter_node = ll2.head if ll2_len <= ll1_len else ll1.head len_diff = abs(ll1_len - ll2_len) if len_diff != 0: while len_diff != 0: longer_node = longer_node.next len_diff -= 1 while longer_node.next != None: if longer_node == shortter_node: return longer_node longer_node = longer_node.next shortter_node = shortter_node.next ll1 = LinkedList([3, 1, 5, 9, 7, 2, 1]) ll2 = LinkedList([4, 6]) ll2.tail.next = ll1.head.next.next.next.next ll2.tail = ll1.tail print(Intersection(ll1, ll2)) ll11 = LinkedList([3, 1, 5, 9, 7, 2, 1]) ll22 = LinkedList([5, 1, 5, 9, 7, 2, 1]) print(Intersection(ll11, ll22))
while True: if slower_node is faster_node: return faster_node # Collision Node slower_node = slower_node.next if faster_node.next is None or faster_node.next.next is None: return False faster_node = faster_node.next.next def detectLoop(ll): collision_node = hasLoop(ll) if not collision_node: return False start_node = ll.head start_node_2 = collision_node while True: if start_node.next is start_node_2.next: return start_node_2 start_node = start_node.next start_node_2 = start_node_2.next ll1 = LinkedList(['A', 'B', 'C', 'D', 'E', 'Q', 'W', 'E', 'R', 'T', 'Y']) ll1.tail.next = ll1.head.next.next.next ll1.tail = None print(detectLoop(ll1))