From 94894cf4bd31d9f5ec97b0b765475a2aee919085 Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Tue, 10 Sep 2024 23:46:55 +0530 Subject: [PATCH] Create 2807. Insert Greatest Common Divisors in Linked List --- ...rt Greatest Common Divisors in Linked List | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 2807. Insert Greatest Common Divisors in Linked List 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; + } +};