Skip to content

Commit 4454118

Browse files
authored
Merge pull request #895 from 0xff-dev/2807
Add solution and test-cases for problem 2807
2 parents ced6762 + b5c37ee commit 4454118

File tree

5 files changed

+69
-9
lines changed

5 files changed

+69
-9
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# [2807.Insert Greatest Common Divisors in Linked List][title]
2+
3+
## Description
4+
Given the `head` of a linked list head, in which each node contains an integer value.
5+
6+
Between every pair of adjacent nodes, insert a new node with a value equal to the **greatest common divisor** of them.
7+
8+
Return the linked list after insertion.
9+
10+
The **greatest common divisor** of two numbers is the largest positive integer that evenly divides both numbers.
11+
12+
**Example 1:**
13+
14+
![1](./ex1_copy.png)
15+
16+
```
17+
Input: head = [18,6,10,3]
18+
Output: [18,6,6,2,10,1,3]
19+
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).
20+
- We insert the greatest common divisor of 18 and 6 = 6 between the 1st and the 2nd nodes.
21+
- We insert the greatest common divisor of 6 and 10 = 2 between the 2nd and the 3rd nodes.
22+
- We insert the greatest common divisor of 10 and 3 = 1 between the 3rd and the 4th nodes.
23+
There are no more adjacent nodes, so we return the linked list.
24+
```
25+
26+
**Example 2:**
27+
28+
![2](./ex2_copy1.png)
29+
30+
```
31+
Input: head = [7]
32+
Output: [7]
33+
Explanation: The 1st diagram denotes the initial linked list and the 2nd diagram denotes the linked list after inserting the new nodes.
34+
There are no pairs of adjacent nodes, so we return the initial linked list.
35+
```
36+
37+
## 结语
38+
39+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
40+
41+
[title]: https://leetcode.com/problems/insert-greatest-common-divisors-in-linked-list
42+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func gcd2807(a, b int) int {
4+
for b != 0 {
5+
a, b = b, a%b
6+
}
7+
return a
8+
}
9+
10+
type ListNode struct {
11+
Val int
12+
Next *ListNode
13+
}
14+
15+
func Solution(head *ListNode) *ListNode {
16+
a, b := head, head.Next
17+
for b != nil {
18+
node := &ListNode{Val: gcd2807(a.Val, b.Val)}
19+
a.Next = node
20+
node.Next = b
21+
a, b = b, b.Next
22+
}
23+
return head
524
}

leetcode/2801-2900/2807.Insert-Greatest-Common-Divisors-in-Linked-List/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs *ListNode
14+
expect *ListNode
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", &ListNode{Val: 18, Next: &ListNode{Val: 6, Next: &ListNode{Val: 10, Next: &ListNode{Val: 3}}}},
17+
&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}}}}}}}},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}
Loading
Loading

0 commit comments

Comments
 (0)