Created
April 22, 2015 03:30
-
-
Save songpu2015617/e917b568f3360e2ab486 to your computer and use it in GitHub Desktop.
cc150 2.5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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