-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_gcd_linked_list.py
More file actions
26 lines (25 loc) · 945 Bytes
/
Copy pathinsert_gcd_linked_list.py
File metadata and controls
26 lines (25 loc) · 945 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
# 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]:
t = head
pre = None
Next = None
while t and t.next:
print(t.val,t.next.val)
if t.val % min(t.val,t.next.val) == 0 and t.next.val % min(t.val,t.next.val) == 0:
pre = t.next
t.next = ListNode(min(t.val,t.next.val))
t.next.next = pre
else:
for i in range(min(t.val,t.next.val),0,-1):
if t.val % i == 0 and t.next.val % i == 0:
pre = t.next
t.next = ListNode(i)
t.next.next = pre
break
t = t.next.next
return head