Implement an algorithm to detect a cycle in a linked list, optimizing for both time and memory complexity.