Problem :

https://leetcode.com/problems/linked-list-cycle-ii/


My Solution :

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def detectCycle(selfhead):
        p1 = p2 = head
        while True:
            try:
                p1 = p1.next
                p2 = p2.next.next
            except:
                return
            if p1 is p2:
                p2 = head
                while p1 is not p2:
                    p1 = p1.next
                    p2 = p2.next
                return p1


Comment :

예전에 풀었던 문제와 같은 원리로 접근하였다. 토끼와 거북이 알고리즘.


https://en.wikipedia.org/wiki/Cycle_detection


2019/01/25 - [프로그래밍/LeetCode] - [LeetCode][Python3] 287. Find the Duplicate Number

secret