def ordered_insertion(self, data): current = self.head previous = None new_node = Node(data) while current and current.data < data: previous = current current = current.next_node new_node.next_node = previous.next_node previous.next_node = new_node
# Find Loop in the Linked List from LinkedList import Node # Creating Circular Linked List h = Node(8) g = Node(7, h) f = Node(6, g) e = Node(5, f) d = Node(4, e) c = Node(3, d) b = Node(2, c) a = Node(1, b) h.next_node = c def tortoise_hare(head): """ Also known as Floyd Cycle Finding Algorithm :param head: :return: """ tortoise = hare = head while hare is not None: tortoise = tortoise.next_node hare = hare.next_node.next_node if hare is tortoise: print(f"Cycle found at {tortoise.data}") break
return curr if __name__ == "__main__": l1 = Node(1) l2 = Node(2) l3 = Node(3) l4 = Node(4) l5 = Node(5) l6 = Node(6) l7 = Node(7) l8 = Node(8) l1.next_node = l2 l2.next_node = l3 l3.next_node = l4 l4.next_node = l5 l5.next_node = l6 l6.next_node = l7 l7.next_node = l8 print(l1) l9 = Node(9) l10 = Node(10) l9.next_node = l10 l10.next_node = l7