From eadd4a28bda5d751e04064f05e24a3e72f7c89f3 Mon Sep 17 00:00:00 2001 From: pawan Date: Tue, 27 Feb 2024 08:40:37 +0530 Subject: [PATCH] Day 12: Attempt 2: 141_Linked_List_Cycle --- Readme.md | 1 + .../Linked List/141_Linked_List_Cycle.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 top_interview_questions/75_hard/Linked List/141_Linked_List_Cycle.py diff --git a/Readme.md b/Readme.md index 061993e..0590097 100644 --- a/Readme.md +++ b/Readme.md @@ -50,6 +50,7 @@ Remember, the 75 Hard Challenge is about personal growth and discipline. Stay co - Day 11: [155. Min Stack](https://leetcode.com/problems/min-stack/) - Day 11: [150. Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) +- Day 12: [141. Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) diff --git a/top_interview_questions/75_hard/Linked List/141_Linked_List_Cycle.py b/top_interview_questions/75_hard/Linked List/141_Linked_List_Cycle.py new file mode 100644 index 0000000..469ce5a --- /dev/null +++ b/top_interview_questions/75_hard/Linked List/141_Linked_List_Cycle.py @@ -0,0 +1,17 @@ +from typing import Optional +# 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: + tortoise = head + hare = head + while hare and hare.next: + tortoise = tortoise.next + hare = hare.next.next + if tortoise == hare: + return True + return False \ No newline at end of file