leetcode1-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 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.

Example

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

自己解法:C++

/**
 * 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) {
        ListNode* a=l1,*f=l2;
        int b=0;
        while(l1!=NULL&&l2!=NULL){//while开始------------------------
            l1->val+=l2->val+b;
            if(l1->val>=10){
                b=1;
                l1->val-=10;
            }
            else{
                b=0;
            }
            
            l1=l1->next;
            l2=l2->next;
        }//while结束-------------------------------------
        if(l1==NULL&&l2==NULL){//---------------------------------1------------
            if(b==1){
                ListNode* d=new ListNode(1);
               
                ListNode* c=a,*e;
                while(c!=NULL){
                    e=c;
                    c=c->next;
                }
                e->next=d;
            }
        }
        else if(l1==NULL){//--------------------------------------2----------------------------
            ListNode* c=a,*e;
            while(c!=NULL){
                    e=c;
                    c=c->next;
                }
           e->next=l2;
            if(b==1){
                while(l2->val+1>=10&&l2->next!=NULL){
                    l2->val=0;
                    l2=l2->next;
                }
                if(l2->next==NULL){
                    if(l2->val+1>=10){
                        ListNode* d=new ListNode(1);
                        l2->val=0;
                        l2->next=d;
                    }
                    else{
                        l2->val+=1;
                    }
                    
                }
                else{
                    l2->val+=1;
                }
            }
            
        }
        else{//---------------------------------------------------3---------------------------------
            if(b==1){
                while(l1->val+1>=10&&l1->next!=NULL){
                    l1->val=0;
                    l1=l1->next;
                }
                if(l1->next==NULL){
                    if(l1->val+1>=10){
                        ListNode* d=new ListNode(1);
                        l1->val=0;
                        l1->next=d;
                    }
                    else{
                        l1->val+=1;
                    }
                    
                }
                else{
                    l1->val+=1;
                }
            }
        }
        
        return a;
    }
};

自己的思路:直接将和放在l1链表中,,只是要考虑到俩组不同长度时候加1不断进位问题。比如:1和9999999999

官方解法:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    ListNode dummyHead = new ListNode(0);
    ListNode p = l1, q = l2, curr = dummyHead;
    int carry = 0;
    while (p != null || q != null) {
        int x = (p != null) ? p.val : 0;
        int y = (q != null) ? q.val : 0;
        int sum = carry + x + y;
        carry = sum / 10;
        curr.next = new ListNode(sum % 10);
        curr = curr.next;
        if (p != null) p = p.next;
        if (q != null) q = q.next;
    }
    if (carry > 0) {
        curr.next = new ListNode(carry);
    }
    return dummyHead.next;
}
}

问:自己遇到的问题及我和官方如何解决的

答:1,考虑最后一位的进位问题,我是用if判断大于10才加,这样就比较麻烦,,,官方直接用%取余后留在本节点,/除数之后加给下一次,,然后最后;2,我是先处理两个链表同样长度的部分,然后处理不一样长的部分,这样的问题就是会导致处理不一样长的时候出现重复的代码。。。官方则是反方向,当不一样长的时候,直接加0。


猜你喜欢

转载自blog.csdn.net/u011776818/article/details/80794420
今日推荐