본문 바로가기

알고리즘/팁

그래프에서 사이클 찾는 법

반응형

해시 테이블 이용

노드를 해시 테이블에 넣고 그 노드가 다시 방문하면 사이클 존재

투 포인터

slow = head
fast = head.next

slow는 한칸씩 전진, fast는 두칸씩 전진한다 fast가 slow와 만나면 사이클 존재 만나지 않고 fast가 null 혹은 fast.next가 null이라면 사이클 존재 x

 

https://leetcode.com/problems/linked-list-cycle/

 

Linked List Cycle - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

'알고리즘 > ' 카테고리의 다른 글

[Python] 위상정렬  (0) 2020.10.07
[Python] 시간초과가 난다면  (0) 2020.09.28
[Python] set 소소한 팁  (0) 2020.09.28
JAVA EOF 판단  (0) 2020.04.24
특정 구간에 0~9의 숫자 갯수 찾기  (0) 2020.03.18