一次PASS呀~痛哭流涕
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
int GCD(int A, int B)
{
int max = (A>B)? A:B;
int min = (A<B)? A:B;
while (max%min !=0)
{
int tmp = max%min;
max= min;
min = tmp;
}
return min;
}
struct ListNode* insertGreatestCommonDivisors(struct ListNode* head) {
struct ListNode* p1=head;
struct ListNode* p2= p1->next;
while (p1->next != NULL)
{
p2 = p1->next;
struct ListNode* new = calloc (1, sizeof(struct ListNode));
new->val = GCD(p1->val, p2->val);
new->next = p2;
p1->next = new;
p1 = p2;
}
return head;
}
沒有留言:
張貼留言