상위 150개 인터뷰쉬움

연결리스트 사이클

'연결된 목록 순환' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

쉬움

연결된 목록의 머리인 head가 주어지면 연결된 목록에 순환이 있는지 확인합니다.

다음 포인터를 계속 따라가면 다시 도달할 수 있는 노드가 목록에 있는 경우 연결 목록에는 순환이 있습니다. 내부적으로 pos는 tail의 다음 포인터가 연결된 노드의 인덱스를 나타내는 데 사용됩니다. pos는 매개변수로 전달되지 않습니다.

연결리스트에 순환이 있으면 true를 반환합니다. 그렇지 않으면 false를 반환합니다.

입력은 값 목록과 정수 pos(꼬리가 연결되는 인덱스, 주기가 없는 경우 -1)로 제공됩니다. hasCycle(head: list, pos: int) -> bool 함수를 구현하세요.

제약
  • The number of nodes in the list is in the range [0, 10000]
  • -100000 <= Node.val <= 100000
  • pos is -1 or a valid index in the linked list

Example 1
Input
[3,2,0,-4], 1
Output
True
Explanation

There is a cycle: the tail node (-4) connects back to the node at index 1 (value 2).

Example 2
Input
[1,2], 0
Output
True
Explanation

There is a cycle: the tail node (2) connects back to the node at index 0 (value 1).

Example 3
Input
[1], -1
Output
False
Explanation

There is no cycle in the list.

Need a Hint?
세트나 힙과 같은 연결 목록 관련 데이터 구조를 사용하는 것을 고려해보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

편집기에서 열기
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.