题意:
链表相加,注意进位。没给数据范围,得不断new节点出来。可能这样更适应企业的要求吧,给范围更适应ACM。
代码实现:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int up = 0;
ListNode* res = new ListNode(0);
ListNode* cur = res;
while( l1!=NULL || l2!=NULL ){
up = 0;
int n1 = 0;
if( l1 != NULL ){
n1 = l1->val;
l1 = l1->next;
}
int n2 = 0;
if( l2 != NULL ){
n2 = l2->val;
l2 = l2->next;
}
cur->val = cur->val+n1+n2;
if( cur->val >= 10 ){
up = cur->val/10;
cur->val = cur->val%10;
}
if( l1!=NULL || l2!=NULL || up!=0 ){
cur->next = new ListNode(up);
cur = cur->next;
}
}
return res;
}