NOTE
Add Two Numbers
LeetCode notes on Add Two Numbers.
This is a historical learning note and may contain outdated or incomplete understanding.
1. Problem Description
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each node stores a single digit.
Add the two numbers and return the sum as a linked list in the same format.
You may assume the two numbers do not contain leading zeros, except for the number 0 itself.
2. Approach
- Approach 1
- Ignore integer overflow
- Convert the two linked lists to integers, add them, then convert the result back to a linked list
- This implementation reads the linked lists with the most significant digit first, which is inconsistent with the reverse-order representation defined by this LeetCode problem; it is retained only as a historical implementation
- Approach 2
- The digits are stored in reverse order, so reverse the corresponding linked-list representation
- Add from right to left to obtain the final result
- Reverse the result
- Approach 3
- Same as Add Strings
3. Implementation
3.1. Ignore Integer Overflow
// Time: O(N)
// Space: O(1)
func addInList(head1 *ListNode, head2 *ListNode) *ListNode {
if head1 == nil {
return head2
}
if head2 == nil {
return head1
}
val1 := 0
for head1 != nil {
val1 = val1*10 + head1.Val
head1 = head1.Next
}
val2 := 0
for head2 != nil {
val2 = val2*10 + head2.Val
head2 = head2.Next
}
sum := val1 + val2
if sum == 0 {
return &ListNode{Val: 0}
}
var head *ListNode
for sum != 0 {
current := sum % 10
head = &ListNode{
Val: current,
Next: head,
}
sum /= 10
}
return head
}
3.2. Stack
package main
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
lst1 := listToSlice(l1)
lst2 := listToSlice(l2)
reverse(lst1)
reverse(lst2)
dummyHead := &ListNode{
}
i := len(lst1) - 1
j := len(lst2) - 1
jinwei := 0
for i >= 0 || j >= 0 || jinwei > 0 {
a := 0
b := 0
if i >= 0 {
a = lst1[i]
i--
}
if j >= 0 {
b = lst2[j]
j--
}
val := (a + b + jinwei) % 10
dummyHead.Next = &ListNode{
Val: val,
Next: dummyHead.Next,
}
jinwei = (a + b + jinwei) / 10
}
head := dummyHead.Next
dummyHead.Next = nil
return reverseList(head)
}
func reverseList(head *ListNode) *ListNode {
if head == nil {
return nil
}
var prev *ListNode
current := head
next := head.Next
for current != nil {
current.Next = prev
prev = current
current = next
if next != nil {
next = next.Next
}
}
return prev
}
func reverse(lst []int) {
left := 0
right := len(lst) - 1
for left < right {
lst[left], lst[right] = lst[right], lst[left]
left++
right--
}
}
func listToSlice(head *ListNode) []int {
lst := make([]int, 0)
current := head
for current != nil {
lst = append(lst, current.Val)
current = current.Next
}
return lst
}
3.3. Traverse the Linked Lists
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
if l1 == nil {
return l2
}
if l2 == nil {
return l1
}
h1 := l1
h2 := l2
dummyHead := &ListNode{Val:-1}
d := dummyHead
jinwei := 0
for h1 != nil || h2 != nil || jinwei > 0 {
sum := 0
if h1 != nil {
sum += h1.Val
h1 = h1.Next
}
if h2 != nil {
sum += h2.Val
h2 = h2.Next
}
sum += jinwei
d.Next = &ListNode{Val:sum%10}
d = d.Next
jinwei = sum / 10
}
return dummyHead.Next
}
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub