From 05af159f5e75aa5fed48e7a963afedcc19ce742a Mon Sep 17 00:00:00 2001 From: sricharan200 <148656478+sricharan200@users.noreply.github.com> Date: Thu, 16 Nov 2023 13:22:33 -0500 Subject: [PATCH 1/3] Create Middle_of_the_Linked_List.py --- Python/Middle_of_the_Linked_List.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Python/Middle_of_the_Linked_List.py diff --git a/Python/Middle_of_the_Linked_List.py b/Python/Middle_of_the_Linked_List.py new file mode 100644 index 00000000..3fd47cbf --- /dev/null +++ b/Python/Middle_of_the_Linked_List.py @@ -0,0 +1,16 @@ +#Time Complexity: O(n) +#Space Complexity: O(1) + +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]: + slow = head + fast = head + while fast and fast.next: + slow = slow.next + fast = fast.next.next + return slow + From 56e47ba3e1cee1e67487fd20d46ae5a40424f8e6 Mon Sep 17 00:00:00 2001 From: sricharan200 <148656478+sricharan200@users.noreply.github.com> Date: Thu, 16 Nov 2023 13:29:56 -0500 Subject: [PATCH 2/3] Update and rename Middle_of_the_Linked_List.py to Linked_List_Cycle.py --- Python/Linked_List_Cycle.py | 24 ++++++++++++++++++++++++ Python/Middle_of_the_Linked_List.py | 16 ---------------- 2 files changed, 24 insertions(+), 16 deletions(-) create mode 100644 Python/Linked_List_Cycle.py delete mode 100644 Python/Middle_of_the_Linked_List.py diff --git a/Python/Linked_List_Cycle.py b/Python/Linked_List_Cycle.py new file mode 100644 index 00000000..d132e68a --- /dev/null +++ b/Python/Linked_List_Cycle.py @@ -0,0 +1,24 @@ +#Time: 95.48% +#Space: 99.98% +#Difficulty: Easy +#Acceptance: 49.0% + +# 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: + if not head or not head.next: + return False + + slow = head + fast = head.next + while slow != fast: + if not fast or not fast.next: + return False + slow = slow.next + fast = fast.next.next + return True diff --git a/Python/Middle_of_the_Linked_List.py b/Python/Middle_of_the_Linked_List.py deleted file mode 100644 index 3fd47cbf..00000000 --- a/Python/Middle_of_the_Linked_List.py +++ /dev/null @@ -1,16 +0,0 @@ -#Time Complexity: O(n) -#Space Complexity: O(1) - -# class ListNode: -# def __init__(self, val=0, next=None): -# self.val = val -# self.next = next -class Solution: - def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]: - slow = head - fast = head - while fast and fast.next: - slow = slow.next - fast = fast.next.next - return slow - From 1f3b3003de452cf182d0af4ca7676302d5f239f1 Mon Sep 17 00:00:00 2001 From: sricharan200 <148656478+sricharan200@users.noreply.github.com> Date: Thu, 16 Nov 2023 13:32:43 -0500 Subject: [PATCH 3/3] Update Linked_List_Cycle.py --- Python/Linked_List_Cycle.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Python/Linked_List_Cycle.py b/Python/Linked_List_Cycle.py index d132e68a..89e1ea1c 100644 --- a/Python/Linked_List_Cycle.py +++ b/Python/Linked_List_Cycle.py @@ -11,14 +11,23 @@ class Solution: def hasCycle(self, head: Optional[ListNode]) -> bool: + # Return False if the list is empty or has only one node (no cycle possible) if not head or not head.next: return False + # Initialize two pointers: 'slow' moves one step at a time, 'fast' moves two steps slow = head fast = head.next + + # Iterate through the list while slow != fast: + # If 'fast' reaches the end of the list (or its next step is None), there's no cycle if not fast or not fast.next: return False + + # Move 'slow' one step forward and 'fast' two steps forward slow = slow.next fast = fast.next.next + + # If 'slow' and 'fast' meet, there is a cycle in the list return True