Skip to content

Latest commit

 

History

History
 
 

445. Add Two Numbers II

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.

Example:

Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7

Related Topics:
Linked List

Similar Questions:

Solution 1. Reverse + Add + Reverse

// OJ: https://leetcode.com/problems/add-two-numbers-ii/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
    ListNode *reverse(ListNode *h) {
        ListNode dummy;
        while (h) {
            auto p = h;
            h = h->next;
            p->next = dummy.next;
            dummy.next = p;
        }
        return dummy.next;
    }
    ListNode* add(ListNode* a, ListNode* b) {
        int carry = 0;
        ListNode dummy, *tail = &dummy;
        while (a || b || carry) {
            if (a) {
                carry += a->val;
                a = a->next;
            }
            if (b) {
                carry += b->val;
                b = b->next;
            }
            tail->next = new ListNode(carry % 10);
            tail = tail->next;
            carry /= 10;
        }
        return dummy.next;
    }
public:
    ListNode* addTwoNumbers(ListNode* a, ListNode* b) {
        a = reverse(a);
        b = reverse(b);
        return reverse(add(a, b));
    }
};

Solution 2.

// OJ: https://leetcode.com/problems/add-two-numbers-ii/
// Author: github.com/lzl124631x
// Time: O(A + B)
// Space: O(1)
class Solution {
    int getLength(ListNode *head) {
        int ans = 0;
        for (; head; head = head->next) ++ans;
        return ans;
    }
    ListNode *reverseList(ListNode *head) {
        ListNode dummy;
        while (head) {
            auto node = head;
            head = head->next;
            node->next = dummy.next;
            dummy.next = node;
        }
        return dummy.next;
    }
public:
    ListNode* addTwoNumbers(ListNode* a, ListNode* b) {
        int la = getLength(a), lb = getLength(b);
        if (la < lb) swap(a, b), swap(la, lb);
        int diff = la - lb;
        ListNode head;
        while (a) {
            int carry = a->val;
            a = a->next;
            if (diff) --diff;
            else {
                carry += b->val;
                b = b->next;
            }
            auto node = new ListNode(carry % 10);
            node->next = head.next;
            head.next = node;
            carry /= 10;
            while (carry && node->next) {
                carry += node->next->val;
                node->next->val = carry % 10;
                carry /= 10;
                node = node->next;
            }
            if (carry) node->next = new ListNode(carry);
        }
        return reverseList(head.next);
    }
};