From 9d83c0cf0ded20c27cfd00c95e6ffbf6d0a31470 Mon Sep 17 00:00:00 2001 From: thispath98 <l0641@naver.com> Date: Wed, 5 Feb 2025 22:38:42 +0900 Subject: [PATCH] feat: Add Linked List Cycle solutions --- linked-list-cycle/thispath98.py | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 linked-list-cycle/thispath98.py diff --git a/linked-list-cycle/thispath98.py b/linked-list-cycle/thispath98.py new file mode 100644 index 000000000..6773b7467 --- /dev/null +++ b/linked-list-cycle/thispath98.py @@ -0,0 +1,34 @@ +# 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: + """ + Intuition: + 노드마다 고유한 id를 저장하고 중복되는 id가 있다면 + True를 반환한다. 그 외에는 False이다. + + Time Complexity: + O(N): + 각 노드를 한번씩 스캔하므로 O(N)이 소요된다. + + Space Complexity: + O(N): + 각 노드의 id를 저장하므로 O(N)이 소요된다. + """ + node_ids = [] + node = head + answer = False + while node: + node_id = id(node) + if node_id not in node_ids: + node_ids.append(node_id) + else: + answer = True + break + node = node.next + + return answer