1. 程式人生 > >[CareerCup] 2.5 Add Two Numbers 兩個數字相加

[CareerCup] 2.5 Add Two Numbers 兩個數字相加

2.5 You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.
EXAMPLE
Input: (7-> 1 -> 6) + (5 -> 9 -> 2).That is, 617 + 295.
Output: 2 -> 1 -> 9.That is, 912.
FOLLOW UP
Suppose the digits are stored in forward order. Repeat the above problem.
EXAMPLE
Input: (6 -> 1 -> 7) + (2 -> 9 -> 5).That is, 617 + 295.
Output: 9 -> 1 -> 2.That is, 912.

跟那道LeetCode有所不同的是,這道題還有個Follow Up,把連結串列存的數字方向變了,原來是表頭存最低位,現在是表頭存最高位。既然是翻轉了連結串列,那麼一種直接的解法是把兩個輸入連結串列都各自翻轉一下,然後用之前的方法相加完成,再把得到的結果翻轉一次,就是結果了,翻轉連結串列的方法可以參見我之前的部落格Reverse Linked List 倒置連結串列。程式碼如下:

解法一:

// Follow up
class Solution {
public:
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
        ListNode 
*dummy = new ListNode(-1); ListNode *cur = dummy; int carry = 0; l1 = reverseList(l1); l2 = reverseList(l2); while (l1 || l2) { int n1 = l1 ? l1->val : 0; int n2 = l2 ? l2->val : 0; int sum = n1 + n2 + carry; carry
= sum / 10; cur->next = new ListNode(sum % 10); cur = cur->next; if (l1) l1 = l1->next; if (l2) l2 = l2->next; } if (carry) cur->next = new ListNode(1); return reverseList(dummy->next); } ListNode *reverseList(ListNode *head) { if (!head) return head; ListNode *dummy = new ListNode(-1); dummy->next = head; ListNode *cur = head; while (cur->next) { ListNode *tmp = cur->next; cur->next = tmp->next; tmp->next = dummy->next; dummy->next = tmp; } return dummy->next; } };

如果我們不採用翻轉連結串列的方法該怎麼做呢,這就比較複雜了。首先我們要縣分別計算出兩個連結串列的長度,然後給稍短一點的連結串列前面補0,補到和另一個連結串列相同的長度。由於要從低位開始相加,而低位是連結串列的末尾,所以我們採用遞迴來處理,先遍歷到連結串列的末尾,然後從後面相加,進位標示符carry用的是引用,這樣保證了再遞歸回溯時值可以正確傳遞,每次計算的節點後面接上上一次回溯的節點,直到回到首節點完成遞迴。最後還是處理最高位的進位問題。程式碼如下:

解法二:

// Follow up
class Solution {
public:
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
        int n1 = 0, n2 = 0, carry = 0;;
        n1 = getLength(l1);
        n2 = getLength(l2);
        if (n1 > n2) l2 = padList(l2, n1 - n2);
        if (n2 > n1) l1 = padList(l1, n2 - n1);
        ListNode *res = addTwoNumbersDFS(l1, l2, carry);
        if (carry == 1) {
            ListNode *tmp = new ListNode(1);
            tmp->next = res;
            res = tmp;
        }
        return res;
    }
    ListNode *addTwoNumbersDFS(ListNode *l1, ListNode *l2, int &carry) {
        if (!l1 && !l2) return NULL;
        ListNode *list = addTwoNumbersDFS(l1->next, l2->next, carry);
        int sum = l1->val + l2->val + carry;
        ListNode *res = new ListNode(sum % 10);
        res->next = list;
        carry = sum / 10;
        return res;
    }
    ListNode *padList(ListNode *list, int len) {
        ListNode *dummy = new ListNode(-1);
        ListNode *cur = dummy;
        for (int i = 0; i < len; ++i) {
            cur->next = new ListNode(0);
            cur = cur->next;
        }
        cur->next = list;
        return dummy->next;
    }
    int getLength(ListNode *list) {
        ListNode *cur = list;
        int res = 0;
        while (cur) {
            ++res;
            cur = cur->next;
        }
        return res;
    }
};