-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0002_add_two_numbers.go
53 lines (48 loc) · 1.27 KB
/
0002_add_two_numbers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// https://leetcode.com/problems/add-two-numbers
//
// 0002. Add Two Numbers [Medium]
//
// You are given two non-empty linked lists representing two non-negative
// integers. The digits are stored in reverse order, and each of their nodes
// contains a single digit. Add the two numbers and return the sum as a linked
// list.
//
// You may assume the two numbers do not contain any leading zero, except the
// number 0 itself.
//
// Constraints:
//
// The number of nodes in each linked list is in the range [1, 100].
// 0 <= Node.val <= 9
// It is guaranteed that the list represents a number that does not have leading zeros.
package leetcode
import "leetcode/linkedlist"
func AddTwoNumbers(list1, list2 *linkedlist.Node[int]) *linkedlist.Node[int] {
list3 := new(linkedlist.Node[int])
temp := list3
overflow := false
for list1 != nil || list2 != nil {
list3.Next = new(linkedlist.Node[int])
list3 = list3.Next
if list1 != nil {
list3.Value += list1.Value
list1 = list1.Next
}
if list2 != nil {
list3.Value += list2.Value
list2 = list2.Next
}
if overflow {
list3.Value++
overflow = false
}
if list3.Value > 9 {
list3.Value -= 10
overflow = true
}
}
if overflow {
list3.Next = &linkedlist.Node[int]{Value: 1, Next: nil}
}
return temp.Next
}