# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param head, a ListNode
# @return a list node
def detectCycle(self, head):
if not head: return None
slow=fast=head
while fast and fast.next:
slow=slow.next
fast=fast.next.next
if fast==slow:
new_point=head
while new_point!=slow:
new_point=new_point.next
slow=slow.next
return new_point
return None
Tuesday, September 9, 2014
Leetcode: Linked List Cycle II @Python
Very interesting problem. Do the math and figure out where the fast and slow pointer met in the previous version of the problem will lead to the solution.
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment