add new solutions
This commit is contained in:
parent
0c73608ce5
commit
d798d5e8c9
19 changed files with 661 additions and 4 deletions
35
solutions/0/q2/solution.go
Normal file
35
solutions/0/q2/solution.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package q2
|
||||
|
||||
type ListNode struct {
|
||||
Val int
|
||||
Next *ListNode
|
||||
}
|
||||
|
||||
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
|
||||
retRoot := &ListNode{}
|
||||
|
||||
p := retRoot
|
||||
carry := 0
|
||||
for l1 != nil || l2 != nil {
|
||||
val := carry
|
||||
if l1 != nil {
|
||||
val += l1.Val
|
||||
l1 = l1.Next
|
||||
}
|
||||
if l2 != nil {
|
||||
val += l2.Val
|
||||
l2 = l2.Next
|
||||
}
|
||||
|
||||
carry, val = val/10, val%10
|
||||
node := &ListNode{Val: val}
|
||||
p.Next = node
|
||||
p = node
|
||||
}
|
||||
if carry > 0 {
|
||||
p.Next = &ListNode{Val: carry}
|
||||
}
|
||||
return retRoot.Next
|
||||
}
|
||||
|
||||
var _ = addTwoNumbers
|
||||
Loading…
Add table
Add a link
Reference in a new issue