In a low-level systems component such as a Meta device runtime, linked structures may become corrupted and form loops. Implement a function that returns whether a singly linked list contains a cycle.
You are given the head of a singly linked list. Each node has:
val: an integer valuenext: a reference to the next node or NoneReturn:
True if following next pointers eventually revisits a previously seen nodeFalse otherwiseYour solution should avoid modifying the list.
Example 1
head = [3, 2, 0, -4], pos = 1True1, so traversal loops forever.Example 2
head = [1, 2], pos = 0TrueExample 3
head = [1], pos = -1False[0, 10^4]-10^5 <= Node.val <= 10^5pos is used only to describe the test setup and is not passed to the function