We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ed9122b commit 646813aCopy full SHA for 646813a
2501-3000/2807.py
@@ -0,0 +1,26 @@
1
+# Definition for singly-linked list.
2
+# class ListNode:
3
+# def __init__(self, val=0, next=None):
4
+# self.val = val
5
+# self.next = next
6
+class Solution:
7
+ def insertGreatestCommonDivisors(self, head: Optional[ListNode]) -> Optional[ListNode]:
8
+ def gcd(a,b):
9
+ if b==0:
10
+ return a
11
+ return gcd(b,a%b)
12
+
13
+ dummy = ListNode(next=head)
14
+ prev = dummy
15
+ cur = dummy.next
16
17
+ while cur:
18
+ midNode = ListNode(val=math.gcd(prev.val,cur.val), next=prev.next)
19
+ prev.next = midNode
20
+ prev = cur
21
+ cur = cur.next
22
23
+ return dummy.next.next
24
25
26
0 commit comments