lass Solution {
public ListNode insertGreatestCommonDivisors(ListNode head) {
if(head==null || head.next==null) return head;
ListNode p=head;
ListNode q=head.next;
while(q!=null){ //注意下不是q.next
int cur = func(p.val,q.val);
// System.out.println(p.val+" "+q.val);
ListNode tmp =new ListNode();
tmp.next=p.next;
p.next=tmp;
tmp.val=cur;
p=q;
q=q.next;
}
return head;
}
public int func(int p, int q){ //无视p,q大小直接求gcd
return q==0?p:func(q,p%q);
}
}
L2807
于 2023-09-20 15:59:09 首次发布