Skip to content

Commit 7349059

Browse files
committed
Add solution and test-cases for problem 2816
1 parent 09a1dcb commit 7349059

File tree

5 files changed

+66
-9
lines changed

5 files changed

+66
-9
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# [2816.Double a Number Represented as a Linked List][title]
2+
3+
## Description
4+
You are given the `head` of a **non-empty** linked list representing a non-negative integer without leading zeroes.
5+
6+
Return the `head` of the linked list after **doubling** it.
7+
8+
**Example 1:**
9+
10+
![1](./example.png)
11+
12+
```
13+
Input: head = [1,8,9]
14+
Output: [3,7,8]
15+
Explanation: The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
16+
```
17+
18+
**Example 2:**
19+
20+
![2](./example2.png)
21+
22+
```
23+
Input: head = [9,9,9]
24+
Output: [1,9,9,8]
25+
Explanation: The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
26+
```
27+
28+
## 结语
29+
30+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
31+
32+
[title]: https://leetcode.com/problems/double-a-number-represented-as-a-linked-list
33+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type ListNode struct {
4+
Val int
5+
Next *ListNode
6+
}
7+
8+
func Solution(head *ListNode) *ListNode {
9+
if head == nil {
10+
return nil
11+
}
12+
var do func(*ListNode, *int)
13+
do = func(cur *ListNode, cf *int) {
14+
if cur == nil {
15+
return
16+
}
17+
do(cur.Next, cf)
18+
v := cur.Val*2 + *cf
19+
*cf = v / 10
20+
v %= 10
21+
cur.Val = v
22+
}
23+
cf := 0
24+
do(head, &cf)
25+
if cf != 0 {
26+
node := &ListNode{Val: cf, Next: head}
27+
return node
28+
}
29+
return head
530
}

leetcode/2801-2900/2816.Double-a-Number-Represented-as-a-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: 1, Next: &ListNode{Val: 8, Next: &ListNode{Val: 9}}}, &ListNode{Val: 3, Next: &ListNode{Val: 7, Next: &ListNode{Val: 8}}}},
17+
{"TestCase2", &ListNode{Val: 9, Next: &ListNode{Val: 9, Next: &ListNode{Val: 9}}}, &ListNode{Val: 1, Next: &ListNode{Val: 9, Next: &ListNode{Val: 9, Next: &ListNode{Val: 8}}}}},
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)