2807. 在链表中插入最大公约数

题目链接:https://leetcode.cn/problems/insert-greatest-common-divisors-in-linked-list/description/

JavaScript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var insertGreatestCommonDivisors = function(head) {
let cur = head;
while(cur.next != null) {
let insertNode = new ListNode(gcd(cur.val, cur.next.val));
insertNode.next = cur.next;
cur.next = insertNode;
cur = insertNode.next;
}
return head;
};
function gcd(a,b){
if(b === 0) return a;
return gcd(b,a % b);
}

2807. 在链表中插入最大公约数
https://pisces34.github.io/2023/08/08/leetcode/2807/
发布于
2023年8月8日
许可协议