문제링크: https://leetcode.com/problems/linked-list-cycle/
주어진 linked list에 cycle이 있는지 없는지 확인하는 문제이다. Cycle이 있으면 return True, 없으면 return False를 하면 된다.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
Data: 각 노드에 저장된 value 값
Next: 해당 노드에 연결된 다음 노드
rtype = False
current = head
while (current):
if current.val == None:
rtype = True
break
else:
current.val = None
current = current.next
rtype = False
current = head
fcurrent = head
while (fcurrent and fcurrent.next):
current = current.next
fcurrent = fcurrent.next.next
if (current == fcurrent):
rtype = True
break