Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#897 cycle detection in linked list #910

Merged
merged 3 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
class ListNode:
def __init__(self, x):
self.val = x
self.next = None

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

# Use Floyd's Cycle-Finding Algorithm
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False

# Helper function to create a linked list with a cycle
def createLinkedList(values, pos):
head = ListNode(values[0])
current = head
nodes = [head] # store nodes to connect cycle later

for value in values[1:]:
new_node = ListNode(value)
current.next = new_node
current = new_node
nodes.append(new_node)

# Create cycle if pos is not -1
if pos != -1:
current.next = nodes[pos]

return head

# Helper function to display the result
def printResult(head, pos):
solution = Solution()
has_cycle = solution.hasCycle(head)
print(f"Cycle at position {pos}: {'Cycle detected' if has_cycle else 'No cycle detected'}")

# Example usage
values = [3, 2, 0, -4]
pos = 1 # means the last node connects to the node at index 1
head = createLinkedList(values, pos)
print("Input linked list values:", values)
printResult(head, pos)

# Test with no cycle
values_no_cycle = [1, 2]
pos_no_cycle = -1 # no cycle
head_no_cycle = createLinkedList(values_no_cycle, pos_no_cycle)
print("\nInput linked list values:", values_no_cycle)
printResult(head_no_cycle, pos_no_cycle)
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Linked List Cycle Detection

## Problem Statement
Given the `head` of a linked list, determine if the linked list contains a cycle. A cycle exists if a node in the list points back to an earlier node, creating a loop in the list. Internally, a variable `pos` represents the index of the node that the tail's `next` pointer is connected to. If there is no cycle, `pos` is `-1`.

Return `true` if the linked list has a cycle, otherwise return `false`.

### Example

Given the linked list:

- **Input**: `head = [3,2,0,-4], pos = 1`
- **Output**: `true`

### Constraints
- The number of nodes in the list is in the range `[0, 10^4]`.
- `-10^5 <= Node.val <= 10^5`
- `pos` is `-1` if there is no cycle.

## Solution Approach

### Floyd’s Cycle Detection Algorithm (Tortoise and Hare)
This approach uses two pointers (`slow` and `fast`) to detect a cycle:

1. **Initialize**:
- Set `slow` and `fast` to `head`.

2. **Traverse**:
- Move `slow` by one step (`slow = slow->next`) and `fast` by two steps (`fast = fast->next->next`) in each iteration.

3. **Cycle Check**:
- If there’s a cycle, `slow` and `fast` will eventually meet at some point within the loop.
- If `fast` reaches the end (`NULL`), there is no cycle.

This algorithm efficiently checks for cycles in `O(n)` time complexity with `O(1)` space complexity.

2 changes: 2 additions & 0 deletions Project-Structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
* Linked List
* Clone A List With Random Pointers
* [Program](Algorithms_and_Data_Structures/Linked%20List/Clone_a_List_with_Random_Pointers/program.py)
* Detect Cycle In List
* [Program](Algorithms_and_Data_Structures/Linked%20List/Detect_Cycle_in_List/program.py)
* [Menu Driven Code For Circular Doubly Linkedlist](Algorithms_and_Data_Structures/Linked%20List/Menu_Driven_Code_for_Circular_Doubly_LinkedList.py)
* [Menu Driven Code For Circular Linkedlist](Algorithms_and_Data_Structures/Linked%20List/Menu_Driven_Code_for_Circular_LinkedList.py)
* [Menu Driven Code For Doubly Linkedlist](Algorithms_and_Data_Structures/Linked%20List/Menu_Driven_Code_for_Doubly_LinkedList.py)
Expand Down