[LeetCode] Linked List Cycle 파이썬 (is operator, id)
2가지 풀이법(1: fast & slow, 2: marked as read) 풀이 1 (fast, slow) class ListNode: def __init__(self, x): self.val = x self.next = None class Solution: # @param head, a ListNode # @return a boolean def hasCycle(self, head): fast, slow = head, head while fast and fast.next: fast, slow = fast.next.next, slow.next if fast is slow: return True return False 어떻게 풀어야 할지 전혀 감이 안왔던 문제 fast, slow라는 2개의 head 의 c..
2021. 4. 1.