Last active
February 24, 2017 06:43
-
-
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?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" 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