-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem2807.py
More file actions
34 lines (27 loc) · 929 Bytes
/
Problem2807.py
File metadata and controls
34 lines (27 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# math is already imported in leetcode
import math
# Both optional and listnode classes are implemented for testing purposes
class Optional:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
pass
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
# arguments are different to the real problem, you must fix for it to work
def insertGreatestCommonDivisors(self, head: ListNode) -> ListNode:
node1 = head
node2 = head
# iterating through the list node
while node2:
# finding the greatest divisor between two values
gcdValue = math.gcd(node1.val, node2.val)
gcdNode = ListNode(gcdValue)
node1.next = gcdNode
gcdNode.next = node2
node1 = node2
node2 = node2.next
return head