1. 程式人生 > >劍指offer 55. 連結串列中環的入口結點

劍指offer 55. 連結串列中環的入口結點

題目描述

給一個連結串列,若其中包含環,請找出該連結串列的環的入口結點,否則,輸出null。

思路:

z這種題直接引入Python list進行遍歷儲存,簡直不能再省心。

參考答案:

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    def EntryNodeOfLoop(self, pHead):
        # write code here
        if not pHead:
            return None
        temp = []
        while pHead:
            if pHead in temp:
                return pHead
            else:
                temp.append(pHead)
                pHead = pHead.next
        return None