Your question is Detect Cycle in Linked List. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
At Slack, a service may receive malformed linked-list structures from an in-memory cache. Write a function that determines whether a singly linked list contains a cycle.
A cycle exists if following next pointers eventually revisits a previously seen node instead of reaching None.
Implement a function that takes the head of a singly linked list and returns a boolean:
head, the first node of a singly linked list, or NoneTrue if the list contains a cycle, otherwise FalseEach node has:
val: integer valuenext: reference to the next node or Nonenext pointer to another node or Nonedef has_cycle(head):