> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Insert Greatest Common Divisors in Linked List

> Tested Python solution for LeetCode 2807 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 2807, Medium. Topics: Linked List, Math, Number Theory. [View on LeetCode](https://leetcode.com/problems/insert-greatest-common-divisors-in-linked-list/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2807   # by problem number
lcpy gen -s insert_greatest_common_divisors_in_linked_list   # by problem name
```

## Problem

Given the `head` of a linked list, return the list after inserting the **greatest common divisor** of each pair of adjacent nodes.

Between every pair of adjacent nodes, insert a new node with a value equal to the greatest common divisor of them.

The **greatest common divisor** of two numbers is the largest positive integer that evenly divides both numbers.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2023/07/18/ex1_copy.png)

```
Input: head = [18,6,10,3]
Output: [18,6,6,2,10,1,3]
Explanation:
- We insert the greatest common divisor of 18 and 6 = 6 between the 1st and the 2nd nodes.
- We insert the greatest common divisor of 6 and 10 = 2 between the 2nd and the 3rd nodes.
- We insert the greatest common divisor of 10 and 3 = 1 between the 3rd and the 4th nodes.
There are no more adjacent nodes, so we return the linked list.
```

![Example 2](https://assets.leetcode.com/uploads/2023/07/18/ex2_copy1.png)

```
Input: head = [7]
Output: [7]
Explanation: The 1st diagram denotes the initial linked list and the 2nd diagram denotes the linked list after inserting the new nodes.
There are no pairs of adjacent nodes, so we return the initial linked list.
```

### Constraints

* The number of nodes in the list is in the range `[1, 5000]`.
* `1 <= Node.val <= 1000`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/insert_greatest_common_divisors_in_linked_list/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/insert_greatest_common_divisors_in_linked_list/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from math import gcd

from leetcode_py import ListNode


class Solution:
    # Time: O(n)
    # Space: O(1)
    def insert_greatest_common_divisors(self, head: ListNode[int] | None) -> ListNode[int] | None:
        current = head
        while current and current.next:
            inserted = ListNode[int](gcd(current.val, current.next.val))
            inserted.next = current.next
            current.next = inserted
            current = inserted.next
        return head
```

## Complexity

| Time | Space |
| ---- | ----- |
| O(n) | O(1)  |

## Tags

[NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
