Create detect_cycle_II.py

This commit is contained in:
marina 2023-08-02 17:06:05 -07:00 committed by GitHub
parent 5eaeb66493
commit 654bb8c76f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,27 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
def detectCycle(self, head):
seen = set()
node = head
while node is not None:
if node in seen:
return node
else:
seen.add(node)
node = node.next
return None