Skip to content

Instantly share code, notes, and snippets.

@Se7soz
Last active January 3, 2016 05:18
Show Gist options
  • Select an option

  • Save Se7soz/8414371 to your computer and use it in GitHub Desktop.

Select an option

Save Se7soz/8414371 to your computer and use it in GitHub Desktop.
Read the How to prepare for an interview series at my blog: http://se7so.blogspot.com/2014/01/how-to-prepare-for-interview.html
node* sum(node* num1, node* num2) {
node* l1 = num1, *l2 = num2;
node* res = new node(0);
int rem = 0;
while(l1 && l2) {
int num = l1->d+l2->d+rem;
if(num >= 10) {
insert(res, num-10);
rem = num/10;
}
else {
rem = 0;
insert(res, num);
}
l1 = l1->nxt;
l2 = l2->nxt;
}
node* tmp = l1 ? l1 : l2;
while(tmp) {
int num = tmp->d+rem;
insert(res, num-10);
rem = num/10;
tmp = tmp->nxt;
}
if(rem)
insert(res, rem);
tmp = res;
res = res->nxt;
delete tmp;
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment