Skip to main content

Linked List Cycle

EasyPremium

Given the head of a linked list, write a function hasCycle to determine if the linked list has a cycle in it. A linked list is said to have a cycle if a node's next pointer points to a previous node in the list, forming a loop. Return true if there is a cycle, otherwise return false.

Example

Linked List Cycle

Can you come up with a solution with O(n) time complexity?

Can you come up with a solution with O(1) space complexity?