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

法一:

/**
 * 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;
    }
};

法二:

/**
 * 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;
    }
};

相关推荐

  1. 207. 课程表

    2024-01-07 09:34:03       63 阅读
  2. 2407-mysql笔记

    2024-01-07 09:34:03       20 阅读
  3. Programming Studio COSC2803

    2024-01-07 09:34:03       23 阅读
  4. 207. Course Schedule

    2024-01-07 09:34:03       71 阅读
  5. leetcode 207.课程表

    2024-01-07 09:34:03       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-07 09:34:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-07 09:34:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-07 09:34:03       82 阅读
  4. Python语言-面向对象

    2024-01-07 09:34:03       91 阅读

热门阅读

  1. HDU 2841:Visible Trees ← 容斥原理

    2024-01-07 09:34:03       62 阅读
  2. 【PHP】TP5使用orderRaw 方法设置排序规则

    2024-01-07 09:34:03       63 阅读
  3. 轮转数组【数组】

    2024-01-07 09:34:03       67 阅读
  4. js中浅拷贝和深拷贝的区别

    2024-01-07 09:34:03       59 阅读