New update is available. Click here to update.

Topics

You are given a Singly Linked List of integers. Return true if it has a cycle, else return false.

A cycle occurs when a node's next points back to a previous node in the list.

```
In the given linked list, there is a cycle, hence we return true.
```

Detailed explanation

```
1 2 3 4 -1
1
```

```
true
```

```
The linked list given in the input is as follows:
```

```
1 2 3 4 -1
0
```

```
true
```

```
The linked list given in the input is as follows:
```

```
5 -1
-1
```

```
false
```

```
The linked list given in the input is as follows:
```

```
Try to solve this problem in O(n).
```

```
Try to solve this problem in O(1).
```

```
0 <= n <= 10^6
-1 <= pos < n
-10^9 <= data <= 10^9 and data != -1
Where 'n' is the size of the singly linked list, 'pos' represents the position (0-indexed) in the linked list where the tail connects to, and 'data' is the Integer data of the singly linked list.
Time Limit: 1 sec
```