Skip to content

Instantly share code, notes, and snippets.

@alexspark
Last active February 24, 2017 06:43
Show Gist options
  • Save alexspark/97018d540fc4d4f06562d57ce47d6675 to your computer and use it in GitHub Desktop.
Save alexspark/97018d540fc4d4f06562d57ce47d6675 to your computer and use it in GitHub Desktop.
How do you find if there is a loop in a singly linked list?
""" Given a singly linked list, how do you find if there is a loop?
assumptions:
- `head` can be None or the first node in a linked list
"""
def is_loop(head):
if head is None or head.next is None:
return false
else:
first, second = head, head.next
while second is not None:
first = first.next
second = second.next.next
if first is second:
return true
return false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment