-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_two_numbers.go
41 lines (39 loc) · 943 Bytes
/
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
package linked_list
import "github.com/anirudhology/leetcode-go/problems/util"
func AddTwoNumbers(l1 *util.ListNode, l2 *util.ListNode) *util.ListNode {
// Dummy head of the resultant list
dummy := &util.ListNode{}
// Pointer to traverse the resultant list
temp := dummy
// Carry
carry := 0
// Process both lists together
for l1 != nil && l2 != nil {
sum := carry + l1.Val + l2.Val
carry = int(sum / 10)
temp.Next = &util.ListNode{Val: sum % 10}
temp = temp.Next
l1 = l1.Next
l2 = l2.Next
}
// For remaining nodes
for l1 != nil {
sum := carry + l1.Val
carry = int(sum / 10)
temp.Next = &util.ListNode{Val: sum % 10}
temp = temp.Next
l1 = l1.Next
}
for l2 != nil {
sum := carry + l2.Val
carry = int(sum / 10)
temp.Next = &util.ListNode{Val: sum % 10}
temp = temp.Next
l2 = l2.Next
}
// Adjust the remaining carry
if carry != 0 {
temp.Next = &util.ListNode{Val: carry}
}
return dummy.Next
}