leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
2807.cpp (447B)
0 class Solution { 1 int gcd(int a, int b) { 2 if (!a) return b; 3 if (!b) return a; 4 return gcd(b, a % b); 5 } 6 7 public: 8 ListNode *insertGreatestCommonDivisors(ListNode *head) { 9 ListNode *crnt = head, *next = head->next; 10 while (next) { 11 crnt->next = new ListNode(gcd(crnt->val, next->val), next); 12 crnt = next; 13 next = next->next; 14 } 15 return head; 16 } 17 };