1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
class Solution { public ListNode insertGreatestCommonDivisors(ListNode head) { ListNode cur = head; while (cur.next != null) { ListNode next = cur.next; int val = calc(cur.val, next.val); ListNode x = new ListNode(val, next); cur.next = x; cur = next; } return head; }
private int calc(int a, int b) { int min = Math.min(a, b); int res = 1; for (int i = 1; i <= min; ++i) { if (a % i == 0 && b % i == 0) res = i; } return res; } }
|