LeetCode발행일 2025. 2. 24.원본 https://blog.naver.com/jword_/223770656038 ↗

Linked List의 Cycle 찾기

Linked List의 Cycle 찾기 — #LinkedListCycle #링크드리스트사이클 #링크드리스트 #개발자의도구들 only 파이썬 목표 참고 : 여기 ...

#LeetCode#Naver Blog

#LinkedListCycle #링크드리스트사이클 #링크드리스트 #개발자의도구들

​

​

  • only 파이썬
  • 목표 참고 : 여기

트리의 균형 확인하기

LeetCode(esay 141. Linked List Cycle) 51.9%

text 코드 예제
                                    [] -> [] -> [] -> [] -> [] -> []
      †________________________|

링크드 리스트가 사이클을 가지고 있는지 확인하기.

​

기본 해결책

text 코드 예제
                                    ✅ 각 Node를 Set에 저장하기

>>> node를 순회한다.
>>> set에 없으면 넣는다
>>> set에 있으면 사이클이 있는 것 -> return True
>>> head == None이 되면 tail로 이동 된 것이다. 이때 끝낸다.
python 코드 예제
                                    # Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def hasCycle(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """

        node_set = set()

        while 1:
            if head == None:
                return False

            if head not in node_set:
                node_set.add(head)
                head = head.next
            else:
                return True

# buit-in function?
  • 제출했더니 가장 빠른 정답 처리가 되었다.
  • 얻을게 더 없는지 공부해보자.

​