LintCode: 带环链表 II

LintCode: 带环链表 II

""" Definition of ListNode class ListNode(object): def __init__(self, val, next=None): self.val = val self.next = next """
class Solution:
    """ @param head: The first node of the linked list. @return: The node where the cycle begins. if there is no cycle, return null """
    def detectCycle(self, head):
        # write your code here
        L = []
        p1 = head
        while p1 != None:
            if p1 not in L:
                L.append(p1)
                p1 = p1.next
            else:
                return p1
        return None

你可能感兴趣的:(链表)