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;
    }
};
相关推荐
旺仔老馒头.19 分钟前
【数据结构与算法】手撕排序算法(二)
c语言·数据结构·算法·排序算法
好学且牛逼的马31 分钟前
【Hot100 | 2 LeetCode49 字母异位词分组问题】
算法
2301_7951672042 分钟前
Rust 在内存安全方面的设计方案的核心思想是“共享不可变,可变不共享”
算法·安全·rust
czhc11400756631 小时前
Java117 最长公共前缀
java·数据结构·算法
java 乐山1 小时前
蓝牙网关(备份)
linux·网络·算法
云泽8081 小时前
快速排序算法详解:hoare、挖坑法、lomuto前后指针与非递归实现
算法·排序算法
数字化脑洞实验室1 小时前
智能决策算法的核心原理是什么?
人工智能·算法·机器学习
流烟默1 小时前
机器学习中拟合、欠拟合、过拟合是什么
人工智能·算法·机器学习
Brianna Home1 小时前
现代C++:从性能泥潭到AI基石
开发语言·c++·算法
再卷也是菜1 小时前
算法基础篇(10)递归型枚举与回溯剪枝
算法·深度优先·剪枝