Skip to content

Latest commit

 

History

History
115 lines (105 loc) · 3.64 KB

File metadata and controls

115 lines (105 loc) · 3.64 KB

题目

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:

imgs/2021-02-21_10-35-27_addtwonumber1.jpg

Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.

Example 2:

Input: l1 = [0], l2 = [0]
Output: [0]

Example 3:

Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]

Constraints:

  • The number of nodes in each linked list is in the range [1, 100].
  • 0 <= Node.val <= 9
  • It is guaranteed that the list represents a number that does not have leading zeros.

思路:

  • 同时遍历两个链表,取出数值进行相加。
  • 对相加的和进行取余数,得到当前节点的值。
  • 对相加的和做除法,得到的值用来判断是否有进位,参与到下一次循环中
  • 别忘了最后一次加法可能产生进位

Java

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int result = 0;
        ListNode dummy = new ListNode(0);
        ListNode current = dummy;
        while (l1!= null || l2 != null) {
            int x = l1 != null ? l1.val : 0;
            int y = l2 != null ? l2.val : 0;
            int sum = result + x + y;
            // 看看要不要进位
            result = sum / 10;
            current.next = new ListNode(sum%10);
            current = current.next;
            if (l1 != null) l1 = l1.next;
            if (l2 != null) l2 = l2.next;
        }
        // 处理还有进位的情况
        if (result > 0) {
            current.next = new ListNode(result);
        }
        return dummy.next;
    }
}

CPP

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* dummy = new ListNode();
        ListNode* cur = dummy;
        int res = 0;
        while (l1 != nullptr || l2 != nullptr) {
            int x = l1 == nullptr ? 0 : l1->val;
            int y = l2 == nullptr ? 0 : l2->val;
            int sum = x + y + res;
            cur->next = new ListNode(sum % 10);
            cur = cur->next;
            res = sum / 10;
         
            if (l1 != nullptr) {
                l1 = l1->next;
            }
         
            if (l2 != nullptr) {
                l2 = l2->next;
            }
        }
        if (res > 0) {
            cur->next = new ListNode(res);
        }
        return dummy->next;
    }
};