1. 程式人生 > >leetcode: 141. Linked List Cycle

leetcode: 141. Linked List Cycle

Difficulty

Easy.

Problem

Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?

AC

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class
Solution(): def hasCycle(self, head): if not head or not head.next: return False slow = fast = head while fast.next and fast.next.next: slow, fast = slow.next, fast.next.next if fast == slow: return True return False