---
# System prepended metadata

title: LC 141. Linked List Cycle
tags: [easy, leedcode, Linked List, python, c++]

---

# LC 141. Linked List Cycle

### [Problem link](https://leetcode.com/problems/linked-list-cycle/)

###### tags: `leedcode` `python` `c++` `easy` `Linked List`

Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.

There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the<code>next</code>pointer. Internally, <code>pos</code>is used to denote the index of the node thattail's<code>next</code>pointer is connected to. **Note that<code>pos</code>is not passed as a parameter** .

Return<code>true</code> if there is a cycle in the linked list. Otherwise, return <code>false</code>.

**Example 1:** 
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
```
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
```

**Example 2:** 
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
```
Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.
```

**Example 3:** 
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
```
Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.
```

 **Constraints:** 

- The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.
- <code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code>
- <code>pos</code> is <code>-1</code> or a  **valid index**  in the linked-list.


 **Follow up:**  Can you solve it using <code>O(1)</code> (i.e. constant) memory?
## Solution 1 - Linked List
#### Python
```python=
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        fast = slow = head

        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next

            if fast == slow:
                return True

        return False
```
#### C++
```cpp=
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        ListNode *fast = head;
        ListNode *slow = head;
        while(fast && fast->next) {
            fast = fast->next->next;
            slow = slow->next;
            if (fast == slow) {
                return true;
            }
        }
        return false;
    }
};
```

>### Complexity
>|             | Time Complexity | Space Complexity |
>| ----------- | --------------- | ---------------- |
>| Solution 1  | O(n)            | O(1)             |


## Note
x
