diff --git a/2807. Insert Greatest Common Divisors in Linked List b/2807. Insert Greatest Common Divisors in Linked List new file mode 100644 index 0000000..3ead5b6 --- /dev/null +++ b/2807. Insert Greatest Common Divisors in Linked List @@ -0,0 +1,19 @@ +class Solution { +public: + ListNode* insertGreatestCommonDivisors(ListNode* head) { + ListNode* f = head; + if (!head->next) { + return head; + } + ListNode* s = head->next; + while (s) { + ListNode* n = new ListNode(); + n->val = gcd(f->val, s->val); + f->next = n; + n->next = s; + f = s; + s = s->next; + } + return head; + } +};