Write an algorithm to detect a cycle in a singly linked list, explaining the time and space complexity of your approach.