Published:
Updated:


Solution (Set)

# Definition for singly-linked list.
from typing import Optional


class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


# LinkedList์—์„œ ์‚ฌ์ดํด์ด ์กด์žฌํ•˜๋ฉด True ๋ฆฌํ„ดํ•˜๋Š” ๋ฌธ์ œ
class Solution:
    # Set ๋ฐฉ์‹
    # time: O(N)
    # Runtime: 55ms
    # Beats: 74.8%
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        # ๋ฌดํ•œ ๋ฃจํ”„์— ๊ฑธ๋ฆฌ์ง€ ์•Š๋„๋ก ์ฃผ์˜ํ•ด์„œ
        # ์‚ฌ์ดํด ๋ถ€๋ถ„ ํŒ๋‹จ ํ›„ ๋ฐ˜๋ณต๋ฌธ ๋น ์ ธ๋‚˜์˜ค๊ฒŒ ํ•˜์ž
        visit_set = set()

        # head๊ฐ€ null์ด ์•„๋‹ˆ๋ฉด ๊ณ„์† ๋ฐ˜๋ณต๋ฌธ ๋Œ์•„์ฃผ๊ธฐ
        while head is not None:
            # head๊ฐ€ visit_set์— ์ด๋ฏธ ์กด์žฌํ•˜๋ฉด True ๋ฆฌํ„ด
            # ์™œ๋ƒ๋ฉด ์ด๋ฏธ ์žˆ๋‹ค๋Š” ๊ฒƒ์€ ๊ทธ ๋…ธ๋“œ๊ฐ€ ์ด๋ฏธ ๋ฐฉ๋ฌธ๋˜์—ˆ๋‹ค๋Š” ๊ฑฐ๋‹ˆ๊นŒ
            if head in visit_set:
                return True

            # set์„ ๊ณ„์† ๋„ฃ์–ด์ค˜์•ผ์ง€ ํŒ๋‹จ์„ ํ•˜์ง€
            # head๊ฐ€ ์ด๋™(.next)ํ•˜๋ฉด์„œ ๊ณ„์† set์— ๊ธฐ๋ณธ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋Š”(add) ๊ฑฐ์•ผ
            visit_set.add(head)
            head = head.next

        # ๋ฐ˜๋ณต๋ฌธ์„ ๋น ์ ธ๋‚˜์™”๋‹ค๋Š” ๊ฒƒ์€ ์ด๋ฏธ null์ด๋ผ๋Š” ๊ฒƒ์ด๊ณ 
        # True๊ฐ€ ์•ˆ ๋˜์—ˆ์œผ๋‹ˆ ๋ฐฉ๋ฌธํ•œ ์ ๋„ ์—†์—ˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ˆ
        # False ๋ฆฌํ„ด
        return False

Solution (Floydโ€™s tortoise and hare)

# Definition for singly-linked list.
from typing import Optional


class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


# LinkedList์—์„œ ์‚ฌ์ดํด์ด ์กด์žฌํ•˜๋ฉด True ๋ฆฌํ„ดํ•˜๋Š” ๋ฌธ์ œ
class Solution:
    # ์ˆœํ™˜ ํƒ์ง€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ
    # Floyd's tortoise and hare ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฐฉ์‹
    # ์ฒซ๋ฒˆ์งธ ํฌ์ธํ„ฐ๋Š” ๊ฑฐ๋ถ์ด์ฒ˜๋Ÿผ ๋Š๋ฆฌ๊ฒŒ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™ํ•˜๊ณ 
    # ๋‘๋ฒˆ์จฐ ํฌ์ธํ„ฐ๋Š” ํ† ๋ผ์ฒ˜๋Ÿผ ๋น ๋ฅด๊ฒŒ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™
    # ๊ทธ๋Ÿผ ์–ธ์  ๊ฐ€๋Š” ๋‘˜์ด ๋งŒ๋‚˜๊ฒŒ ๋จ
    # time: O(N)
    # Runtime: 59ms
    # Beats: 51.97%
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        # ๊ฑฐ๋ถ์ด ํฌ์ธํ„ฐ, ํ† ๋ผ ํฌ์ธํ„ฐ ๊ฐ๊ฐ LinkedList์˜ head๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ํ•˜์ž
        turtle_pointer, rabbit_pointer = head, head

        # ํ† ๋ผ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•˜๋Š” ์ด์œ ๋Š” ํ† ๋ผ๋ฅผ ์ค‘์ ์œผ๋กœ ์ƒ๊ฐํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฐฉ์‹์ด๊ธฐ ๋•Œ๋ฌธ (ํ† ๋ผ ๋จผ์ € ์ง„ํ–‰)
        # next๋ฅผ ์ฒดํฌํ•˜๋Š” ์ด์œ ๋Š” ํ† ๋ผ๊ฐ€ 2์นธ์„ ํ•œ๊บผ๋ฒˆ์— ์ด๋™ํ•˜๊ธฐ ๋•Œ๋ฌธ
        while rabbit_pointer is not None and rabbit_pointer.next is not None:
            # ๊ฑฐ๋ถ์ด๋Š” 1์นธ
            turtle_pointer = turtle_pointer.next
            # ํ† ๋ผ๋Š” 2์นธ
            rabbit_pointer = rabbit_pointer.next.next

            # ๋ง‰ ๋‹ฌ๋ฆฌ๋‹ค๊ฐ€ ๋‘˜์ด ๋งŒ๋‚˜๋ฉด True ๋ฆฌํ„ด
            if turtle_pointer == rabbit_pointer:
                return True

        # ๋ฐ˜๋ณต๋ฌธ ๋น ์ ธ๋‚˜์˜ค๋ฉด ๋‘˜์ด ๋ชป ๋งŒ๋‚˜๊ณ  null์ด ๋œ ๊ฑฐ๋‹ˆ๊นŒ False ๋ฆฌํ„ด
        return False

Leave a comment