Linked List Cycle
Description:
Given a linked list, determine if it has a cycle in it.
Example
Given -21->10->4->5, tail connects to node index 1, return true
Challenge
Follow up:
Can you solve it without using extra space?
Code:
"""
Definition of ListNode
class ListNode(object):
def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param: head: The first node of linked list.
@return: True if it has a cycle, or false
"""
def hasCycle(self, head):
# write your code here
if not head:
return False
tmp = head
while tmp.next:
if tmp.next.val == None:
return True
tmp.val = None
tmp = tmp.next
return False