您的位置:首页 > 其它

【second】Add Two Numbers

2013-10-22 13:40 134 查看
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int carry = 0;
ListNode* nhead=NULL,*prev=NULL;
ListNode* it1=l1,*it2=l2;
while(it1||it2||carry)
{
int num1 = (it1?it1->val:0);
int num2 = (it2?it2->val:0);
int sum = num1+num2+carry;
if(nhead==NULL)
prev = nhead = new ListNode(sum%10);
else
{
prev->next = new ListNode(sum%10);
prev = prev->next;
}
carry = sum/10;
it1 = (it1?it1->next:NULL);
it2 = (it2?it2->next:NULL);
}

return nhead;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: