
g0001_0100.s0002_add_two_numbers.solution.go Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
package s0002_add_two_numbers
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Math #Linked_List #Recursion
// #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15
// #Big_O_Time_O(max(N,M))_Space_O(max(N,M)) #2024_03_05_Time_4_ms_(84.60%)_Space_4.4_MB_(47.97%)
type ListNode struct {
Val int
Next *ListNode
}
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
dummyHead := &ListNode{}
p, q, curr := l1, l2, dummyHead
carry := 0
for p != nil || q != nil {
x, y := 0, 0
if p != nil {
x = p.Val
p = p.Next
}
if q != nil {
y = q.Val
q = q.Next
}
sum := carry + x + y
carry = sum / 10
curr.Next = &ListNode{Val: sum % 10}
curr = curr.Next
}
if carry > 0 {
curr.Next = &ListNode{Val: carry}
}
return dummyHead.Next
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy