
def has_cycle(node):
    if hasattr(node, 'visited'):
        return True
    node.visited = True

    if node.next is None:
        return False
    return has_cycle(node.next)

# TEST CODE
class Node(object):
    def __init__(self, data = None, next_node = None):
        self.data = data
        self.next = next_node


first_case = Node(1)

three = Node(3)
two = Node(2, three)
one = Node(1, two)
three.next = two

second_case = one

x = Node('x')
y = Node('y', x)
third_case = Node('third_case', y)

# print('has_cycle(first_case): {}'.format(has_cycle(first_case)))
print('has_cycle(second_case): {}'.format(has_cycle(second_case)))
# print('has_cycle(second_case): {}'.format(has_cycle(third_case)))


def has_cycle(node):
    c = node
    n = node.next

    while n is not None:
        if hasattr(c, 'visited'):
            return True
        c.visited = True
        c = n.next
        n = c.next
    return False


# TEST CODE
class Node(object):
    def __init__(self, data = None, next_node = None):
        self.data = data
        self.next = next_node


first_case = Node(1)

three = Node(3)
two = Node(2, three)
one = Node(1, two)
three.next = two

second_case = one

x = Node('x')
y = Node('y', x)
third_case = Node('third_case', y)

print('has_cycle(first_case): {}'.format(has_cycle(first_case)))
print('has_cycle(second_case): {}'.format(has_cycle(second_case)))
print('has_cycle(second_case): {}'.format(has_cycle(third_case)))

Solve problem to detect linked list cycle
Solve problem to detect linked list cycle

https://www.hackerrank.com/challenges/ctci-linked-list-cycle

  
  def has_cycle(node):
+     c = node
+     n = node.next
-     if hasattr(node, 'visited'):
-         return True
-     node.visited = True
  
-     if node.next is None:
+     while n is not None:
+         if hasattr(c, 'visited'):
+             return True
+         c.visited = True
+         c = n.next
+         n = c.next
-         return False
+     return False
-     return has_cycle(node.next)
+ 
  
  # TEST CODE
  class Node(object):
      def __init__(self, data = None, next_node = None):
          self.data = data
          self.next = next_node
  
  
  first_case = Node(1)
  
  three = Node(3)
  two = Node(2, three)
  one = Node(1, two)
  three.next = two
  
  second_case = one
  
  x = Node('x')
  y = Node('y', x)
  third_case = Node('third_case', y)
  
- # print('has_cycle(first_case): {}'.format(has_cycle(first_case)))
+ print('has_cycle(first_case): {}'.format(has_cycle(first_case)))
  print('has_cycle(second_case): {}'.format(has_cycle(second_case)))
- # print('has_cycle(second_case): {}'.format(has_cycle(third_case)))
+ print('has_cycle(second_case): {}'.format(has_cycle(third_case)))
  
Solve problem to detect linked list cycle
## Code Before:

def has_cycle(node):
    if hasattr(node, 'visited'):
        return True
    node.visited = True

    if node.next is None:
        return False
    return has_cycle(node.next)

# TEST CODE
class Node(object):
    def __init__(self, data = None, next_node = None):
        self.data = data
        self.next = next_node


first_case = Node(1)

three = Node(3)
two = Node(2, three)
one = Node(1, two)
three.next = two

second_case = one

x = Node('x')
y = Node('y', x)
third_case = Node('third_case', y)

# print('has_cycle(first_case): {}'.format(has_cycle(first_case)))
print('has_cycle(second_case): {}'.format(has_cycle(second_case)))
# print('has_cycle(second_case): {}'.format(has_cycle(third_case)))

## Instruction:
Solve problem to detect linked list cycle
## Code After:

def has_cycle(node):
    c = node
    n = node.next

    while n is not None:
        if hasattr(c, 'visited'):
            return True
        c.visited = True
        c = n.next
        n = c.next
    return False


# TEST CODE
class Node(object):
    def __init__(self, data = None, next_node = None):
        self.data = data
        self.next = next_node


first_case = Node(1)

three = Node(3)
two = Node(2, three)
one = Node(1, two)
three.next = two

second_case = one

x = Node('x')
y = Node('y', x)
third_case = Node('third_case', y)

print('has_cycle(first_case): {}'.format(has_cycle(first_case)))
print('has_cycle(second_case): {}'.format(has_cycle(second_case)))
print('has_cycle(second_case): {}'.format(has_cycle(third_case)))
