leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2807.cpp (447B)


      1 class Solution {
      2     int gcd(int a, int b) {
      3         if (!a) return b;
      4         if (!b) return a;
      5         return gcd(b, a % b);
      6     }
      7 
      8   public:
      9     ListNode *insertGreatestCommonDivisors(ListNode *head) {
     10         ListNode *crnt = head, *next = head->next;
     11         while (next) {
     12             crnt->next = new ListNode(gcd(crnt->val, next->val), next);
     13             crnt = next;
     14             next = next->next;
     15         }
     16         return head;
     17     }
     18 };