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;
    }
};
相关推荐
姜不吃葱10 分钟前
【力扣热题100】双指针—— 接雨水
数据结构·算法·leetcode·力扣热题100
PineappleCoder14 分钟前
大小写 + 标点全搞定!JS 如何精准统计单词频率?
前端·javascript·算法
tanyongxi661 小时前
C++ 特殊类设计与单例模式解析
java·开发语言·数据结构·c++·算法·单例模式
qq_513970441 小时前
力扣 hot100 Day76
算法·leetcode·职场和发展
Moshow郑锴2 小时前
机器学习相关算法:回溯算法 贪心算法 回归算法(线性回归) 算法超参数 多项式时间 朴素贝叶斯分类算法
算法·机器学习·回归
liulilittle2 小时前
BFS寻路算法解析与实现
开发语言·c++·算法·宽度优先·寻路算法·寻路
剪一朵云爱着2 小时前
PAT 1065 A+B and C (64bit)
算法·pat考试
喜欢吃燃面3 小时前
C++算法竞赛:位运算
开发语言·c++·学习·算法
项目申报小狂人3 小时前
算法应用上新!自适应更新策略差分进化算法求解球形多飞行器路径规划问题,附完整MATLAB代码
开发语言·算法·matlab