
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* Head = new ListNode(-1);
ListNode* pre = Head;
int carry=0,sum=0;
while(l1!=NULL || l2!=NULL || carry!=0){
if(l1!=NULL){
carry+=l1->val;
l1=l1->next;
}
if(l2!=NULL){
carry+=l2->val;
l2=l2->next;
}
ListNode* node=new ListNode(carry%10);
pre->next=node;
pre=node;
carry/=10;
}
return Head->next;
}
};
该博客主要围绕使用C++进行两数相加展开,虽未给出具体内容,但核心是利用C++语言实现两数相加的功能,属于信息技术中后端开发的编程实践。
1382

被折叠的 条评论
为什么被折叠?



