๐Ÿ’ซ ETC/Leetcode

Week 1-2. [Linked List] 141. Linked List Cycle (Python3)

ming412 2023. 8. 28. 23:35

๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ์ •

โœ๏ธ ๋‚˜๋งŒ์˜ ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์ œ ์ •๋ฆฌํ•˜๊ธฐ (=์ฒด๊ณ„ํ™”)

https://leetcode.com/problems/linked-list-cycle/?envType=study-plan-v2&envId=top-interview-150

 

Linked List Cycle - LeetCode

Can you solve this real interview question? Linked List Cycle - Given head, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuo

leetcode.com

์ฃผ์–ด์ง„ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์— ์‚ฌ์ดํด์ด ์žˆ๋‹ค๋ฉด true, ์—†๋‹ค๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ผ
  • ์ž…๋ ฅ
    • nodes | ListNode := ๋…ธ๋“œ๋“ค, | nodes | ∈ [0, 10^4]
    • Node.val | int := ๋…ธ๋“œ์˜ ๊ฐ’, | Node.val | ∈ [-10^5, 10^5]
  • ์ถœ๋ ฅ
    • hasCycle | boolean := ์‚ฌ์ดํด์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€
  • ์กฐ๊ฑด
    • pos๋Š” ์œ ํšจํ•œ ์ธ๋ฑ์Šค ํ˜น์€ -1์ด๋‹ค.
      • pos๋Š” ์‚ฌ์ดํด์˜ ์‹œ์ž‘์ ์ด๋‹ค.
      • pos๊ฐ€ -1์ด๋ผ๋ฉด ์‚ฌ์ดํด์ด ์—†๋Š” ๊ฒƒ์ด๋‹ค.

๐Ÿ”Ž ์–ด๋–ค ์‹์œผ๋กœ ์ ‘๊ทผํ–ˆ๋Š”๊ฐ€?

๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐœ๋…์€ ์•Œ๊ณ  ์žˆ์—ˆ์ง€๋งŒ, ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋Š” ์ฒ˜์Œ ํ’€์–ด๋ณด์•˜๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋–ป๊ฒŒ ์ ‘๊ทผํ•ด์•ผํ•  ์ง€ ์ „ํ˜€ ๊ฐ์ด ์žกํžˆ์ง€ ์•Š์•˜๋‹ค.

์ฒซ ๋ฌธ์ œ๋Š” ํ’€์ด๋ฅผ ํ™•์ธํ•˜๊ณ  ์ดํ›„๋ถ€ํ„ฐ ํ˜ผ์ž ํ’€์–ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.

๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ๋ฅผ ํƒ๊ตฌํ•ด๋ณด๊ธฐ

๐ŸŽฏ ๊ตฌํ˜„ ์ฝ”๋“œ

ํ”Œ๋กœ์ด๋“œ์˜ ํ† ๋ผ์™€ ๊ฑฐ๋ถ์ด(Floyd's Tortoise and Hare) ์•Œ๊ณ ๋ฆฌ์ฆ˜

ํ•œ ์นธ์”ฉ ์ด๋™ํ•˜๋Š” slow ํฌ์ธํ„ฐ์™€ ๋‘ ์นธ์”ฉ ์ด๋™ํ•˜๋Š” fast ํฌ์ธํ„ฐ, ๋‘ ๊ฐœ์˜ ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•ด ์‚ฌ์ดํด์„ ์ฐพ๋Š”๋‹ค. (์‚ฌ์ดํด์ด ์žˆ๋‹ค๋ฉด ๋‘ ํฌ์ธํ„ฐ๋Š” ์‚ฌ์ดํด ์•ˆ์—์„œ ์–ธ์  ๊ฐ€๋Š” ๋งŒ๋‚œ๋‹ค.)

  1. head๋ถ€ํ„ฐ slow, fast ํฌ์ธํ„ฐ๋ฅผ ๊ฐ๊ฐ ํ•œ ์นธ, ๋‘ ์นธ์”ฉ ์ด๋™์‹œํ‚จ๋‹ค.
  2. 1๋ฒˆ์„ ๋ฐ˜๋ณตํ•˜๋‹ค๊ฐ€, slow์™€ fast์˜ ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ™์•„์ง„๋‹ค๋ฉด, ์‚ฌ์ดํด์ด ์กด์žฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
  3. ๋งŒ์•ฝ slow ํ˜น์€ fast๊ฐ€ null์— ๋„์ฐฉํ•œ๋‹ค๋ฉด, ์‚ฌ์ดํด์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด๋‹ค.
# 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 head == None: return False
        
        slow, fast = head, head.next
        
        while slow != fast:
            if fast == None or fast.next == None: return False
            slow = slow.next # ํ•œ ์นธ ์ด๋™
            fast = fast.next.next # ๋‘ ์นธ ์ด๋™

        return True

 

โฑ ์„ฑ๋Šฅ ์ธก์ • (์‹œ๊ฐ„๋ณต์žก๋„/๊ณต๊ฐ„๋ณต์žก๋„)

linear search์ด๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(N)์ด๋‹ค.

๋˜ํ•œ ํฌ์ธํ„ฐ(slow, fast) ๋‘ ๊ฐœ๋งŒ ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(1)์ด๋‹ค. 

 

๋А๋‚€ ์ 

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋Š” ์ข…์ข… ํ’€์—ˆ์ง€๋งŒ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ ๋ฌธ์ œ๋Š” ์ฒ˜์Œ์ด๋ผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ head: Optional[ListNode]๋กœ ์ฃผ์–ด์ง€๋Š” ๊ฒƒ์ด ๋‚ฏ์„ค์—ˆ๋‹ค.

ํ† ๋ผ์™€ ๊ฑฐ๋ถ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜๋„ ์ฒ˜์Œ ํ•™์Šตํ•œ ๋‚ด์šฉ์ด์—ˆ๋Š”๋ฐ, ์‚ฌ์ดํด์˜ ์œ ๋ฌด๋ฅผ ํŒ๋‹จํ•  ๋•Œ slow์™€ fast ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋„ ์•Œ์•„๋‘์–ด์•ผ๊ฒ ๋‹ค!

 

์ฐธ๊ณ  ์ž๋ฃŒ

Leetcode 141. Linked List Cycle with Python

LeetCode ์—ฐ๊ฒฐ ๋ชฉ๋ก ์ฃผ๊ธฐ ์†”๋ฃจ์…˜ ์„ค๋ช… - Java

๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์— ์‚ฌ์ดํด ์ฐพ๊ธฐ (ํ”Œ๋กœ์ด๋“œ์˜ ํ† ๋ผ์™€ ๊ฑฐ๋ถ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜)