<p>Given the head of a linked list <code>head</code>, in which each node contains an integer value.</p>

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

<p>Return <em>the linked list after insertion</em>.</p>

<p>The <strong>greatest common divisor</strong> of two numbers is the largest positive integer that evenly divides both numbers.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/18/ex1_copy.png" style="width: 641px; height: 181px;" />
<pre>
<strong>Input:</strong> head = [18,6,10,3]
<strong>Output:</strong> [18,6,6,2,10,1,3]
<strong>Explanation:</strong> The 1<sup>st</sup> diagram denotes the initial linked list and the 2<sup>nd</sup> diagram denotes the linked list after inserting the new nodes (nodes in blue are the inserted nodes).
- We insert the greatest common divisor of 18 and 6 = 6 between the 1<sup>st</sup> and the 2<sup>nd</sup> nodes.
- We insert the greatest common divisor of 6 and 10 = 2 between the 2<sup>nd</sup> and the 3<sup>rd</sup> nodes.
- We insert the greatest common divisor of 10 and 3 = 1 between the 3<sup>rd</sup> and the 4<sup>th</sup> nodes.
There are no more adjacent nodes, so we return the linked list.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/18/ex2_copy1.png" style="width: 51px; height: 191px;" />
<pre>
<strong>Input:</strong> head = [7]
<strong>Output:</strong> [7]
<strong>Explanation:</strong> The 1<sup>st</sup> diagram denotes the initial linked list and the 2<sup>nd</sup> diagram denotes the linked list after inserting the new nodes.
There are no pairs of adjacent nodes, so we return the initial linked list.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the list is in the range <code>[1, 5000]</code>.</li>
	<li><code>1 &lt;= Node.val &lt;= 1000</code></li>
</ul>

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
from collections import defaultdict

class Solution:
    def insertGreatestCommonDivisors(self, head: Optional[ListNode]) -> Optional[ListNode]:
        start_head = head
        
        def get_primes(n):
            primes = []
            for k in range(2, n):
                is_prime = True
                
                for prime in primes:
                    if k % prime == 0:
                        is_prime = False
                        break
                
                if is_prime:
                    primes.append(k)

            return primes
            
        head = start_head
        max_value = 0

        while head is not None:
            max_value = max(max_value, head.val)
            head = head.next
        
        primes = get_primes(max_value)
        # print('PRIMES', primes)

        def factorize(num):
            factors = defaultdict(int)

            for prime in primes:
                while num % prime == 0:
                    factors[prime] += 1
                    num //= prime

                if num == 1:
                    break

            if len(factors) == 0:
                factors[num] += 1

            return factors

        def build_from_factors(factors):
            num = 1
            for factor in factors:
                num *= factor ** factors[factor]

            return num

        def get_common_factors(factors1, factors2):
            new_factors = {}

            for factor in factors1:
                new_factors[factor] = min(factors1[factor], factors2[factor])

            return new_factors

        head = start_head
        prev_head = None

        while head is not None:
            current_head = head

            if prev_head is not None:
                prev_factors = factorize(prev_head.val)
                new_factors = factorize(head.val)
                # print('FACTORS', prev_head, head, prev_factors, new_factors)
                common_factors = get_common_factors(prev_factors, new_factors)
                gcd = build_from_factors(common_factors)

                new_node = ListNode(gcd)
                prev_head.next = new_node
                new_node.next = head

            prev_head = current_head
            head = head.next

        return start_head
212