Skip to main content

2807. Insert Greatest Common Divisors in Linked List - LeetCode

ยท One min read
from ..notes import gcd


class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next


class Solution:
def insertGreatestCommonDivisors(self, head: ListNode) -> ListNode:
node = head
while node.next:
node.next = ListNode(val=gcd(node.val, node.next.val), next=node.next)
node = node.next.next
return head