Skip to content

Commit

Permalink
Linked List Cycle II
Browse files Browse the repository at this point in the history
  • Loading branch information
rinwf committed Sep 11, 2018
1 parent 2178fa7 commit 5262359
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lc142.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None


class Solution(object):
def detectCycle(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head is None or head.next is None:
return None
hk = head.next
west = hk.next
while hk != west:
if west is None or west.next is None:
return None
hk = hk.next
west = west.next.next

hk = head
while hk != west:
hk = hk.next
west = west.next
return hk

0 comments on commit 5262359

Please sign in to comment.