Skip to content

Commit ae1dccf

Browse files
authored
Create 2807. Insert Greatest Common Divisors in Linked List (#581)
2 parents 2ab8fab + 94894cf commit ae1dccf

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
ListNode* insertGreatestCommonDivisors(ListNode* head) {
4+
ListNode* f = head;
5+
if (!head->next) {
6+
return head;
7+
}
8+
ListNode* s = head->next;
9+
while (s) {
10+
ListNode* n = new ListNode();
11+
n->val = gcd(f->val, s->val);
12+
f->next = n;
13+
n->next = s;
14+
f = s;
15+
s = s->next;
16+
}
17+
return head;
18+
}
19+
};

0 commit comments

Comments
 (0)