<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
class Solution:
    def insertGreatestCommonDivisors(self, head: Optional[ListNode]) -> Optional[ListNode]:

        current_node = head

        def get_factor(number1, number2):
            while number2:  #new method
                number1, number2 = number2, number1%number2
            return number1


            #old method
            # li_factor_1 = []
            # li_factor_2 = []

            # for i in range(2, int(math.sqrt(number1))+2):
            #     while number1 % i == 0:
            #         if i not in li_factor_1:
            #             li_factor_1.append(i)
            #         number1 = number1//i
                
            # for j in range(2, int(math.sqrt(number2))+2):
            #     while number2 % j == 0:
            #         if j not in li_factor_2:
            #             li_factor_2.append(j)
            #         number2 = number2//j
            # print(li_factor_1, li_factor_2)
            # lst3 = [value for value in li_factor_1 if value in li_factor_2]
            # factor = 1
            # for num in lst3:
            #     factor*=num
            # return factor

        while current_node != None:
            print("Current_node, ", current_node.val)
            number_1 = current_node.val
            if current_node.next != None:
                print("Next_node, ", current_node.next.val)
                number_2 = current_node.next.val
            else:
                break

            gcd_factor = get_factor(number_1, number_2)
            gcd_factor = ListNode(gcd_factor)
            temp = current_node.next
            # print(temp)
            # print(current_node)
            current_node.next = gcd_factor
            gcd_factor.next = temp
            # print("AFter adding gcd, ",current_node)
            current_node = gcd_factor.next
            # temp = current_node.next
            # current_node.next = gcd_factor
            # gcd_factor.next = temp.next
            # # print(gcd_factor)
            # print(current_node)
            # current_node = temp.next
        return head

            

        
111