From 81550766b8cff319938beab20122a7c4fa429a9a Mon Sep 17 00:00:00 2001 From: Anshika Yadav <14anshika7yadav@gmail.com> Date: Mon, 10 Jun 2024 15:51:54 +0530 Subject: [PATCH 1/2] Added Leetcode Problem 2807: #922 --- ...greatest-common-divisors-in-linked-list.md | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 dsa-solutions/lc-solutions/2500 - 3000/2807-insert-greatest-common-divisors-in-linked-list.md diff --git a/dsa-solutions/lc-solutions/2500 - 3000/2807-insert-greatest-common-divisors-in-linked-list.md b/dsa-solutions/lc-solutions/2500 - 3000/2807-insert-greatest-common-divisors-in-linked-list.md new file mode 100644 index 000000000..f8866d415 --- /dev/null +++ b/dsa-solutions/lc-solutions/2500 - 3000/2807-insert-greatest-common-divisors-in-linked-list.md @@ -0,0 +1,89 @@ +--- +id: insert-greatest-common-divisors-in-linked-list +title:Insert Greatest Common Divisors in Linked List +sidebar_label: 2807 Insert Greatest Common Divisors in Linked List +tags: + - Linked List + - LeetCode + - C++ +description: "This is a solution to the Insert Greatest Common Divisors in Linked List problem on LeetCode." +--- + +## Problem Description + +Given the head of a linked list head, in which each node contains an integer value. + +Between every pair of adjacent nodes, insert a new node with a value equal to the greatest common divisor of them. + +Return the linked list after insertion. + +The greatest common divisor of two numbers is the largest positive integer that evenly divides both numbers. + +### Examples + +**Example 1:** + +``` + +Input: head = [18,6,10,3] +Output: [18,6,6,2,10,1,3] +``` + +**Example 2:** + +``` +Input: root = [7] +Output: [7] +``` + +### Constraints + +- The number of nodes in the list is in the range $[1, 5000]$. +- $-1 \leq \text{Node.val} \leq 1000$. + +### Approach + +To solve this problem(insert greatest common divisors in linked list) we will first store the nodes value of the linked list in vector and then find gcd of each adjacent pair and store it in another vector and then simply iterate the linked list and insert the gcd in the linked list as per the question. + +#### Code in C++ + +```cpp +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* insertGreatestCommonDivisors(ListNode* head) { + vectora,b; + ListNode* k=head; + while(head!=NULL){ + a.push_back(head->val); + head=head->next; + } + for(int i=0;inext!=NULL){ + ListNode* temp=new ListNode(b[i]); + ListNode* next=k->next; + i++; + k->next=temp; + temp->next=next; + k=k->next->next; + } + return head; + } +}; +``` + + From 30f6a18e7ff3717808e9bd8afb4462bc3480e4da Mon Sep 17 00:00:00 2001 From: Anshika Yadav <14anshika7yadav@gmail.com> Date: Mon, 10 Jun 2024 15:57:58 +0530 Subject: [PATCH 2/2] Updated --- ...7-insert-greatest-common-divisors-in-linked-list.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dsa-solutions/lc-solutions/2500 - 3000/2807-insert-greatest-common-divisors-in-linked-list.md b/dsa-solutions/lc-solutions/2500 - 3000/2807-insert-greatest-common-divisors-in-linked-list.md index f8866d415..eeaf5697a 100644 --- a/dsa-solutions/lc-solutions/2500 - 3000/2807-insert-greatest-common-divisors-in-linked-list.md +++ b/dsa-solutions/lc-solutions/2500 - 3000/2807-insert-greatest-common-divisors-in-linked-list.md @@ -1,6 +1,6 @@ --- id: insert-greatest-common-divisors-in-linked-list -title:Insert Greatest Common Divisors in Linked List +title: Insert Greatest Common Divisors in Linked List sidebar_label: 2807 Insert Greatest Common Divisors in Linked List tags: - Linked List @@ -64,17 +64,17 @@ public: vectora,b; ListNode* k=head; while(head!=NULL){ - a.push_back(head->val); + a.push_back(head->val); // store each node value of the given linked list head=head->next; } - for(int i=0;inext!=NULL){ - ListNode* temp=new ListNode(b[i]); + while(k!=NULL && k->next!=NULL){ // inserting gcd between the linked list + ListNode* temp=new ListNode(b[i]); ListNode* next=k->next; i++; k->next=temp;