Skip to content
Rain Hu's Workspace
Go back

[LeetCode] 2. Add Two Numbers

Rain Hu

2. Add Two Numbers


一、題目

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example 1:
addtwonumber1

Example 2:

Example 3:

Constraints:


二、分析

三、解題

1. Recursion

ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    return add(l1, l2, 0);
}
ListNode* add(ListNode* l1, ListNode* l2, int cin){     // 將 cin 設為 carry in
    l1->val += l2->val + cin;
    cin = l1->val / 10;     // 進位
    l1->val %= 10;
    if (l1->next || l2->next || cin != 0){      // 若有下一位數,或 carry in 不等於 0
        l1->next = add((l1->next == NULL ? new ListNode(0) : l1->next),
                       (l2->next == NULL ? new ListNode(0) : l2->next),
                        cin);
    }
    return l1;
}
}

2. Iteration

ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    ListNode* dummy = new ListNode(-1, l1);     // 設置一個 sentinel node
    ListNode* curr = dummy;     // 設置一個在當前位置前的節點來控制資料
    int cin = 0;
    while (l1 || l2 || cin > 0) {   // 當必定有下一位時
        // 借用 l1 或 l2,若兩者皆為空節點,則新建一個
        ListNode* tmp = l1 ? l1 : l2 : l2 : new ListNode(0);
        // 加法器
        tmp->val = (l1 ? l1->val : 0) + (l2 ? l2->val : 0) + cin;
        cin = tmp / 10;
        tmp->val %= 10;
        // 處理當 l1 為空結點的狀況
        if (!l1 && l2) {    // l2 不為空節點時,則借 l2 來用
            curr->next = l2;
        } else {        // 若 l1 跟 l2 都是空節點,建一個新的節點
            curr->next = tmp;
        }
        
        if (l1) l1 = l1->next;  // 前進一個節點
        if (l2) l2 = l2->next;    
        curr = curr->next;
    }
    return dummy->next;     // 記得回傳的是 sentinel->next
}

回目錄 Catalog


Share this post on:

Previous
[LeetCode] 3. Longest Substring Without Repeating Characters
Next
[LeetCode] 1297. Maximum Number of Occurrences of a Substring