LeetCode_2_Add Two Numbers

Description Title:
given two non-empty list is used to represent two non-negative integer. Where their respective bits are stored in reverse order of the way, and they each node can store only one digit. If we add up these two numbers, it will return a new list and to represent them. You can assume that in addition to the numbers 0, these two numbers will not begin with 0.

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

ALGORITHM: The two traverse list from beginning to end, within the meaning of the current node pointer and number of carry and sum modulo give digits. Define a binary variable carry, a carry flag. Finally, you can generate a list of the return.

ListNode *head=NULL,*p=NULL;
        int carry=0;
        while(l1||l2){
            int sum1=l1?l1->val:0;
            int sum2=l2?l2->val:0;
            int sum=sum1+sum2+carry;
            carry=sum/10;
            ListNode *cur=new ListNode(sum%10);
            if(!head)
                head=cur;
            if(p)
                p->next=cur;
            p=cur;
            l1=l1?l1->next:NULL;
            l2=l2?l2->next:NULL;
        }
        if(carry){
            ListNode *l=new ListNode(carry);
            p->next=l;
        }
        return head;

The following is a run-time efficiency chart
Runtime Chart
the way: a long time did not write the list of topics the algorithm, the way a little rusty. The following are just beginning to write a little bug in the program segment

ListNode *l3,*p;
        ListNode *head=new ListNode(0); 
        p=l3=head;
        int carry=0;
        while((l1!=NULL)&&(l2!=NULL)){
            int sum=0;
            sum=l1->val+l2->val+carry;
            carry=sum/10;
            sum%=10;
            ListNode* tempNode=new ListNode(sum);
            l3->next=tempNode;
            l3=l3->next;
            l1=l1->next;
            l2=l2->next;
        }
        while(l1!=NULL){
            ListNode* tempNode=new ListNode(l1->val);
            l3->next=tempNode;
            l1=l1->next;
            l3=l3->next;
        }
        while(l2!=NULL){
            ListNode* tempNode=new ListNode(l2->val);
            l3->next=tempNode;
            l2=l2->next;
            l3=l3->next;
        }
        if(carry!=0){
            ListNode* tempNode=new ListNode(carry);
            l3->next=tempNode;
            l3=l3->next;
        }
        head=head->next;
        delete p;
        return head;

Great God of Internet programming:
algorithm ideology: As a result of the list l1, l1 is the final return of this list. The data l2 l1 is sequentially applied to the corresponding node, l1-> val value> = 10 If the addition, then a carry, if l1-> nextNULL, the storage node need to apply the carry result, air will not enter next bit is added to the val. When l1When NULL, l2 only needs to be connected to the remaining nodes can l1.
This method saves a lot of time, but also saves a lot of space (do not need to apply for a useless node)

class Solution {
public:
  ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    auto l3=l1;
    while(1) {
      if (l1==NULL) {
        return l3;
      }
      if(l2!=NULL) {
        l1->val+=l2->val;
        l2=l2->next;
      }
      if (l1->val>=10) {
        l1->val-=10;
        if (l1->next==NULL) {
          l1->next = new ListNode(1);
        }else {
          l1->next->val++;
        }
      }
      if (l1->next==NULL&&l2!=NULL) {
        l1->next=l2;
        l2=NULL;
      }
      l1=l1->next;
    }
  }
};

Run Time follows:
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/all_about_WZY/article/details/88243039