题解 | #牛牛的冒险旅程#
牛牛的冒险旅程
https://www.nowcoder.com/practice/79f7bf3d985c4b33af6d048ab53848d6
# class ListNode: # def __init__(self, x): # self.val = x # self.next = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head ListNode类 # @return int整型 # class Solution: def gcdInCycle(self , head: ListNode) -> int: # write code here cur=head n=0 list1,list2=[],[] while cur: cur=cur.next n=n+1 cur=head for i in range(n): list1.append(cur.val) if cur.val not in list2: list2.append(cur.val) cur=cur.next if len(list2)==n: return -1 a=min(list2) for i in range(a,0,-1): flag=1 for j in range (len(list2)): if list2[j]%i!=0: flag=0 if flag==1: return i