-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinked List Cycle II.py
37 lines (27 loc) · 964 Bytes
/
Linked List Cycle II.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# https://leetcode.com/problems/linked-list-cycle-ii/submissions/
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def detectCycle(self, head: ListNode) -> ListNode:
# If head is empty, return nothing
if head is None:
return head
# If head is by itself, return itself
if head.next is None:
return None
# Create a list
nodesVisited = list()
# Iterate link list
currNode = head
while currNode != None:
# Check if node has been visited
if currNode in nodesVisited:
return currNode
# Add node to log
nodesVisited.append(currNode)
# Update node
currNode = currNode.next
return None