题解 | #牛牛的冒险旅程#
牛牛的冒险旅程
https://www.nowcoder.com/practice/79f7bf3d985c4b33af6d048ab53848d6
import numbers import re # class ListNode: # def __init__(self, x): # self.val = x # self.next = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head ListNode类 # @return int整型 # from functools import reduce class Solution: def gcdInCycle(self , head: ListNode) -> int: # write code here nums=[] cur=head dic={} while cur: if not dic.get(cur.val): dic[cur.val]=1 else: nums.append(cur.val) dic[cur.val]+=1 if dic[cur.val]==3: return reduce(self.gcd,nums) cur=cur.next return -1 def gcd(self,x,y): while y: x,y=y,x%y return x