mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 17:44:10 +08:00
@ -16,36 +16,24 @@ type ListNode = structures.ListNode
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
|
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
|
||||||
if l1 == nil || l2 == nil {
|
head := &ListNode{Val: 0}
|
||||||
return nil
|
n1, n2, carry, current := 0, 0, 0, head
|
||||||
}
|
for l1 != nil || l2 != nil || carry != 0 {
|
||||||
head := &ListNode{Val: 0, Next: nil}
|
|
||||||
current := head
|
|
||||||
carry := 0
|
|
||||||
for l1 != nil || l2 != nil {
|
|
||||||
var x, y int
|
|
||||||
if l1 == nil {
|
if l1 == nil {
|
||||||
x = 0
|
n1 = 0
|
||||||
} else {
|
} else {
|
||||||
x = l1.Val
|
n1 = 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
|
l1 = l1.Next
|
||||||
}
|
}
|
||||||
if l2 != nil {
|
if l2 == nil {
|
||||||
|
n2 = 0
|
||||||
|
} else {
|
||||||
|
n2 = l2.Val
|
||||||
l2 = l2.Next
|
l2 = l2.Next
|
||||||
}
|
}
|
||||||
}
|
current.Next = &ListNode{Val: (n1 + n2 + carry) % 10}
|
||||||
if carry > 0 {
|
current = current.Next
|
||||||
current.Next = &ListNode{Val: carry % 10, Next: nil}
|
carry = (n1 + n2 + carry) / 10
|
||||||
}
|
}
|
||||||
return head.Next
|
return head.Next
|
||||||
}
|
}
|
||||||
|
@ -51,37 +51,26 @@ package leetcode
|
|||||||
* Next *ListNode
|
* Next *ListNode
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
|
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
|
||||||
if l1 == nil || l2 == nil {
|
head := &ListNode{Val: 0}
|
||||||
return nil
|
n1, n2, carry, current := 0, 0, 0, head
|
||||||
}
|
for l1 != nil || l2 != nil || carry != 0 {
|
||||||
head := &ListNode{Val: 0, Next: nil}
|
|
||||||
current := head
|
|
||||||
carry := 0
|
|
||||||
for l1 != nil || l2 != nil {
|
|
||||||
var x, y int
|
|
||||||
if l1 == nil {
|
if l1 == nil {
|
||||||
x = 0
|
n1 = 0
|
||||||
} else {
|
} else {
|
||||||
x = l1.Val
|
n1 = 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
|
l1 = l1.Next
|
||||||
}
|
}
|
||||||
if l2 != nil {
|
if l2 == nil {
|
||||||
|
n2 = 0
|
||||||
|
} else {
|
||||||
|
n2 = l2.Val
|
||||||
l2 = l2.Next
|
l2 = l2.Next
|
||||||
}
|
}
|
||||||
}
|
current.Next = &ListNode{Val: (n1 + n2 + carry) % 10}
|
||||||
if carry > 0 {
|
current = current.Next
|
||||||
current.Next = &ListNode{Val: carry % 10, Next: nil}
|
carry = (n1 + n2 + carry) / 10
|
||||||
}
|
}
|
||||||
return head.Next
|
return head.Next
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user