2807. Insert Greatest Common Divisors in Linked List

Given the head of a linked list head, in which each node contains an integer value.

Between every pair of adjacent nodes, insert a new node with a value equal to the greatest common divisor of them.

Return the linked list after insertion.

The greatest common divisor of two numbers is the largest positive integer that evenly divides both numbers.

Example 1:

复制代码
Input: head = [18,6,10,3]
Output: [18,6,6,2,10,1,3]
Explanation: The 1st diagram denotes the initial linked list and the 2nd diagram denotes the linked list after inserting the new nodes (nodes in blue are the inserted nodes).
- We insert the greatest common divisor of 18 and 6 = 6 between the 1st and the 2nd nodes.
- We insert the greatest common divisor of 6 and 10 = 2 between the 2nd and the 3rd nodes.
- We insert the greatest common divisor of 10 and 3 = 1 between the 3rd and the 4th nodes.
There are no more adjacent nodes, so we return the linked list.

Example 2:

复制代码
Input: head = [7]
Output: [7]
Explanation: The 1st diagram denotes the initial linked list and the 2nd diagram denotes the linked list after inserting the new nodes.
There are no pairs of adjacent nodes, so we return the initial linked list.

Constraints:

  • The number of nodes in the list is in the range [1, 5000].
  • 1 <= Node.val <= 1000

法一:

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* insertGreatestCommonDivisors(ListNode* head) {
        int count = 1;
        while(head->next == nullptr){
            return head;
        }
        ListNode * node = head;
        while(node -> next != nullptr){
            int a,b;
            if(node -> val >= node -> next -> val){
                a = node -> val;
                b = node -> next -> val;
            }
            else{
                a = node -> next -> val;
                b = node -> val;
            }
            int temp;
            while(b!= 0){
                temp = a;
                a = b;
                b = temp%b;
            }
            node -> next = new ListNode(a,node->next);
            node = node -> next -> next;
        }        
        return head;
    }
};

法二:

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* insertGreatestCommonDivisors(ListNode* head) {
        while(head->next == nullptr){
            return head;
        }
        ListNode * node = head;
        while(node -> next != nullptr){
            node -> next = new ListNode(std::__gcd(node->val, node->next->val),node->next);
            node = node -> next -> next;
        }        
        return head;
    }
};
相关推荐
波力海苔夹心脆67522 分钟前
VisionPro 与 VisionMaster 对比(上):软件定位、开发环境与核心视觉工具对照
图像处理·算法·计算机视觉·c#·视觉检测·个人开发·vp9
动词ing25 分钟前
【题目练习】盛最多水的容器
算法·目标检测
海绵宝宝转agent26 分钟前
2026-10-14算法+面经总结
java·笔记·算法·缓存
虚无的纽扣26 分钟前
【力扣刷题】第四天:三数之和、四数之和
算法·leetcode·职场和发展
ZiLing35 分钟前
2026 ROS 2 Lyrical 工程实例(一):从零搭建 AMR 激光 SLAM——Gazebo、slam_toolbox 与 Nav2 全链路跑通
算法
巴糖36 分钟前
为什么你的大模型总在胡说八道?—— top_k /top_p/temperature 调参实战手册
算法
rou41 分钟前
LeetCode410.分割数组的最大值
算法
垆边人似月.42 分钟前
华为题:字符串中最长连续数字串
开发语言·c++·算法
_不会dp不改名_44 分钟前
leetcode_3273 数位平方和的最大值
算法·leetcode·职场和发展
vivo互联网技术1 小时前
SSR-Merge:把 LoRA 合并从"参数算术"变成"信号路由" | ICML 2026
人工智能·算法·计算机视觉