mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-19 04:23:54 +08:00
52 lines
865 B
Go
52 lines
865 B
Go
package leetcode
|
|
|
|
import (
|
|
"github.com/halfrost/LeetCode-Go/structures"
|
|
)
|
|
|
|
// ListNode define
|
|
type ListNode = structures.ListNode
|
|
|
|
/**
|
|
* Definition for singly-linked list.
|
|
* type ListNode struct {
|
|
* Val int
|
|
* Next *ListNode
|
|
* }
|
|
*/
|
|
|
|
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
|
|
if l1 == nil || l2 == nil {
|
|
return nil
|
|
}
|
|
head := &ListNode{Val: 0, Next: nil}
|
|
current := head
|
|
carry := 0
|
|
for l1 != nil || l2 != nil {
|
|
var x, y int
|
|
if l1 == nil {
|
|
x = 0
|
|
} else {
|
|
x = l1.Val
|
|
}
|
|
if l2 == nil {
|
|
y = 0
|
|
} else {
|
|
y = l2.Val
|
|
}
|
|
current.Next = &ListNode{Val: (x + y + carry) % 10, Next: nil}
|
|
current = current.Next
|
|
carry = (x + y + carry) / 10
|
|
if l1 != nil {
|
|
l1 = l1.Next
|
|
}
|
|
if l2 != nil {
|
|
l2 = l2.Next
|
|
}
|
|
}
|
|
if carry > 0 {
|
|
current.Next = &ListNode{Val: carry % 10, Next: nil}
|
|
}
|
|
return head.Next
|
|
}
|