Learn

As we saw with the merge point problem, more than one node can reference another node. These references can create a cycle in the linked list where the traversal will loop back on itself.

# -> b -> c # / \ \ # a d <- # 'd' node's next points to 'b' node

Write a function that detects whether a cycle exists in a linked list. A cycle exists if traversing the linked list visits the same node more than once.

A cycle does not mean repeated values. Avoid this pitfall in your implementation by comparing the Node instances themselves, not their values!

a = Node('a') other_a = Node('a') a.val == other_a.val # True a == other_a # False

To recap:

  • write a function: has_cycle().
  • has_cycle() takes an instance of LinkedList as the argument.
  • return a Boolean which indicates whether a cycle exists.

Instructions

1.

Solve this problem and pass the tests. If you need help, check out the hint!

Sign up to start coding

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Already have an account?