[LeetCode][Python3] 142. Linked List Cycle II
                2019. 10. 1. 01:15 |
                
                    프로그래밍/LeetCode
                
            
            
            
        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(self, head):
        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
'프로그래밍 > LeetCode' 카테고리의 다른 글
| [LeetCode][Python3] 337. House Robber III (0) | 2019.10.02 | 
|---|---|
| [LeetCode][Python3] 647. Palindromic Substrings (1) | 2019.09.25 | 
| [LeetCode][Python3] 338. Counting Bits (0) | 2019.09.24 | 
| [LeetCode][Python3] 406. Queue Reconstruction by Height (0) | 2019.09.23 | 
| [LeetCode][Python3] 90. Subsets II (0) | 2019.09.17 | 
| [LeetCode][Python3] 739. Daily Temperatures (0) | 2019.09.12 | 
| [LeetCode][Python3] 401. Binary Watch (0) | 2019.09.05 | 
| [LeetCode][Python3] 692. Top K Frequent Words (0) | 2019.09.03 | 
최근에 달린 댓글 최근에 달린 댓글