其实暴力遍历开数组也可以,但不如以下新建链表块的方法简单
int FindCommDivisor(int num1, int num2)
{
int n;
int i;
n = fmin(num1, num2);
for (i = n; i >= 1; i--) {
if (num1 % i == 0 && num2 % i == 0) {
return i;
}
}
return 0;
}
struct ListNode *insertGreatestCommonDivisors(struct ListNode *head)
{
struct ListNode *p1 = head;
while (p1->next) {
struct ListNode *node = malloc(sizeof(struct ListNode));
node->val = FindCommDivisor(p1->val, p1->next->val);
node->next = p1->next;
p1->next = node;
p1 = node->next;
}
return head;
}