Skip to content

Commit

Permalink
Create 2807. Insert Greatest Common Divisors in Linked List (#581)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chayandas07 authored Sep 10, 2024
2 parents 2ab8fab + 94894cf commit ae1dccf
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions 2807. Insert Greatest Common Divisors in Linked List
Original file line number Diff line number Diff line change
@@ -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;
}
};

0 comments on commit ae1dccf

Please sign in to comment.