---
# System prepended metadata

title: 2807. Insert Greatest Common Divisors in Linked List
tags: [Leetcode, Linked List, Medium]

---

Link: https://leetcode.com/problems/insert-greatest-common-divisors-in-linked-list/description/
## Code
```python=
class Solution:
    def insertGreatestCommonDivisors(self, head: Optional[ListNode]) -> Optional[ListNode]:
        currNode = head
        while currNode.next:
            nextNode = currNode.next
            gcdVal = math.gcd(currNode.val, nextNode.val)
            gcdNode = ListNode(gcdVal)
            currNode.next = gcdNode
            gcdNode.next = nextNode
            currNode = nextNode
        return head
```