【leetcode】2. 两数相加

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

示例:

输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/add-two-numbers

解法1:
基于原链表结构,把l2的结构加到l1上,如果l2长于l1 则l1到末尾接l2.
需要判断:(1)进位问题(2)l2长度问题 (3)l1到了末尾,但仍然有进位数,需要新建node作为末尾。代码比较麻烦

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int jinwei = 0;
        ListNode *startnode = l1;
        ListNode *lastnode = l1;
        while (l1 != NULL) {
            lastnode = l1;
            if (l1 != NULL && l2 != NULL)
            {
                int tmp_val = l1->val + l2->val + jinwei;
                jinwei = 0;
                if (tmp_val < 10){
                    l1->val = tmp_val;
                }
                else {
                    l1->val = tmp_val - 10;
                    jinwei = tmp_val / 10;
                    cout << l1->val << " " << jinwei << endl;
                }

                if (l1->next == NULL && l2->next != NULL)
                {

                    l1->next = l2->next;
                    l1 = l1->next;
                    l2 = NULL;
                }
                else 
                {
                    l1 = l1->next;
                    l2 = l2->next;
                }
                
            }

            else if (l1 != NULL && l2 == NULL ){
                int tmp_val = l1->val + jinwei;
                jinwei = 0;
                if (tmp_val < 10){
                    l1->val = tmp_val;
                }
                else {
                    l1->val = tmp_val % 10;
                    jinwei = tmp_val / 10;
                }
                l1 = l1->next;
            }

        }

        
        if (lastnode->next == NULL  &&  jinwei > 0)
        {
            ListNode *newnode = new ListNode(jinwei);
            jinwei = 0;
            lastnode->next = newnode;
        }
        return startnode;
    }



};

解法2:每次新建一个node,加上l1和l2的值,并输出进位。直到l1 val & l2 val & 进位均为空。

需要判断:(1)进位问题。代码比较简洁

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int jinwei = 0;
        ListNode* startnode = NULL;
        ListNode* lastnode = NULL;
        while ( !(l1 == NULL && l2 == NULL && jinwei == 0) ){
            ListNode* newnode = new ListNode(0);
            if (startnode == NULL){
                startnode = newnode;
            }
            if (lastnode == NULL) {
                lastnode = newnode;
            } 
            else {
               lastnode->next = newnode; 
               lastnode = newnode;
            }

            int val = 0;
            if (l1 != NULL){
                val += l1->val;
                l1 = l1->next;
            }
            if (l2 != NULL){
                val += l2->val;
                l2 = l2->next;
            }
            val += jinwei;


            newnode->val = val % 10;
            jinwei = val / 10;

        }
        return startnode;
    }
    
};

你可能感兴趣的:(【leetcode】2. 两数相加)