[Linked List] Linked List Cycle

2021. 2. 9. 22:49프로그래밍-코딩테스트/LeetCode

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 continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.

 

Return true if there is a cycle in the linked list. Otherwise, return false.

 

Linked List가 cycle로 연결되는지 여부를 묻는 문제, pos는 input으로 들어오지 않는다고 한다.

 

방문한 노드를 다시 방문하는지 여부를 점검해주면 된다.

next와 next.next 를 반복으로 설정한다.

 

아래 코드가 좀 더 직관적이다

'프로그래밍-코딩테스트 > LeetCode' 카테고리의 다른 글

[Stack] Min Stack  (0) 2021.02.09
[Linked List] Intersection of Two Linked Lists  (0) 2021.02.09
[Linked List] Palindrome Linked List  (0) 2021.02.09
[Stack] Valid Parentheses  (0) 2021.02.09
[Array] two sum  (0) 2021.02.02