Difference between a LinkedList and a Binary Search Tree

Linked List: Item(1) -> Item(2) -> Item(3) -> Item(4) -> Item(5) -> Item(6) -> Item(7) Binary tree: Node(1) / Node(2) / \ / Node(3) RootNode(4) \ Node(5) \ / Node(6) \ Node(7) In a linked list, the items are linked together through a single next pointer. In a binary tree, each node can have 0, … Read more

How to determine if a linked list has a cycle using only two memory locations

I would suggest using Floyd’s Cycle-Finding Algorithm aka The Tortoise and the Hare Algorithm. It has O(n) complexity and I think it fits your requirements. Example code: function boolean hasLoop(Node startNode){ Node slowNode = Node fastNode1 = Node fastNode2 = startNode; while (slowNode && fastNode1 = fastNode2.next() && fastNode2 = fastNode1.next()){ if (slowNode == fastNode1 … Read more