Skip to content

Latest commit

 

History

History
41 lines (30 loc) · 1.09 KB

_2807. Insert Greatest Common Divisors in Linked List.md

File metadata and controls

41 lines (30 loc) · 1.09 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 13, 2024

Last updated : July 01, 2024


Related Topics : Linked List, Math, Number Theory

Acceptance Rate : 92.39 %


Solutions

Python

# 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]:
        curr = head

        while curr and curr.next :
            curr.next = ListNode(val=gcd(curr.val, curr.next.val), next=curr.next)
            curr = curr.next.next
        return head