Skip to content

Instantly share code, notes, and snippets.

@songpu2015617
Created April 22, 2015 03:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save songpu2015617/e917b568f3360e2ab486 to your computer and use it in GitHub Desktop.
Save songpu2015617/e917b568f3360e2ab486 to your computer and use it in GitHub Desktop.
cc150 2.5
# include <iostream>
using namespace std;
typedef struct node{
int data;
node *next;
}node;
node * addtwonumber(node*l1,node*l2){
if (l1==NULL) return l2;
if (l2==NULL) return l1;
node *res, *pre=NULL;
int carry=0;
while(l1 && l2){
int value = l1->data+l2->data+carry;
node *nd= new node();
nd->data=value%10;
carry=value/10;
if(pre){
pre->next=nd;
pre=nd;
}
else{
res=pre=nd;
}
l1=l1->next;
l2=l2->next;
}
while (l1){
int value=l1->data+carry;
node *nd=new node();
nd->data=value;
pre->next=nd;
pre=nd;
carry= value/10;
l1=l1->next;
}
while (l2){
int value=l2->data+carry;
node *nd=new node();
nd->data=value%10;
pre->next=nd;
pre=nd;
carry= value/10;
l2=l2->next;
}
if (carry>0){
int value =carry;
node *nd=new node();
nd->data=value%10;
pre->next=nd;
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment