diff --git a/leetcode/2801-2900/2807.Insert-Greatest-Common-Divisors-in-Linked-List/README.md b/leetcode/2801-2900/2807.Insert-Greatest-Common-Divisors-in-Linked-List/README.md new file mode 100644 index 000000000..93453cc48 --- /dev/null +++ b/leetcode/2801-2900/2807.Insert-Greatest-Common-Divisors-in-Linked-List/README.md @@ -0,0 +1,42 @@ +# [2807.Insert Greatest Common Divisors in Linked List][title] + +## Description +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:** + +![1](./ex1_copy.png) + +``` +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:** + +![2](./ex2_copy1.png) + +``` +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. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/insert-greatest-common-divisors-in-linked-list +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2801-2900/2807.Insert-Greatest-Common-Divisors-in-Linked-List/Solution.go b/leetcode/2801-2900/2807.Insert-Greatest-Common-Divisors-in-Linked-List/Solution.go index d115ccf5e..aff8e1ea6 100755 --- a/leetcode/2801-2900/2807.Insert-Greatest-Common-Divisors-in-Linked-List/Solution.go +++ b/leetcode/2801-2900/2807.Insert-Greatest-Common-Divisors-in-Linked-List/Solution.go @@ -1,5 +1,24 @@ package Solution -func Solution(x bool) bool { - return x +func gcd2807(a, b int) int { + for b != 0 { + a, b = b, a%b + } + return a +} + +type ListNode struct { + Val int + Next *ListNode +} + +func Solution(head *ListNode) *ListNode { + a, b := head, head.Next + for b != nil { + node := &ListNode{Val: gcd2807(a.Val, b.Val)} + a.Next = node + node.Next = b + a, b = b, b.Next + } + return head } diff --git a/leetcode/2801-2900/2807.Insert-Greatest-Common-Divisors-in-Linked-List/Solution_test.go b/leetcode/2801-2900/2807.Insert-Greatest-Common-Divisors-in-Linked-List/Solution_test.go index 14ff50eb4..4b126575b 100755 --- a/leetcode/2801-2900/2807.Insert-Greatest-Common-Divisors-in-Linked-List/Solution_test.go +++ b/leetcode/2801-2900/2807.Insert-Greatest-Common-Divisors-in-Linked-List/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs *ListNode + expect *ListNode }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", &ListNode{Val: 18, Next: &ListNode{Val: 6, Next: &ListNode{Val: 10, Next: &ListNode{Val: 3}}}}, + &ListNode{Val: 18, Next: &ListNode{Val: 6, Next: &ListNode{Val: 6, Next: &ListNode{Val: 2, Next: &ListNode{Val: 10, Next: &ListNode{Val: 1, Next: &ListNode{Val: 3}}}}}}}}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { } diff --git a/leetcode/2801-2900/2807.Insert-Greatest-Common-Divisors-in-Linked-List/ex1_copy.png b/leetcode/2801-2900/2807.Insert-Greatest-Common-Divisors-in-Linked-List/ex1_copy.png new file mode 100644 index 000000000..3018e7d0d Binary files /dev/null and b/leetcode/2801-2900/2807.Insert-Greatest-Common-Divisors-in-Linked-List/ex1_copy.png differ diff --git a/leetcode/2801-2900/2807.Insert-Greatest-Common-Divisors-in-Linked-List/ex2_copy1.png b/leetcode/2801-2900/2807.Insert-Greatest-Common-Divisors-in-Linked-List/ex2_copy1.png new file mode 100644 index 000000000..cf0fd1b5c Binary files /dev/null and b/leetcode/2801-2900/2807.Insert-Greatest-Common-Divisors-in-Linked-List/ex2_copy1.png differ