``````/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* node = new ListNode(0);
ListNode* p1 = l1;
ListNode* p2 = l2;
ListNode* curr = node;
int carry = 0;

while(p1!=NULL || p2!=NULL){
int x = (p1!=NULL) ? p1->val:0;
int y = (p2!=NULL) ? p2->val:0;
int sum  = carry + x + y;  //x,y加上当前进位
carry =  sum/10;   //进位
curr->next = new ListNode(sum%10);
curr = curr->next;
if(p1!=NULL) p1 = p1->next;
if(p2!=NULL) p2 = p2->next;
}
if(carry > 0)
curr->next = new ListNode(carry);

return node->next;
}
};
``````

[这是我最最开始的想法，虽然较为复杂，但是对于的复杂度还是挺可观的,中间没有创建新的链表]：
l1,l2 以其一较长的链表为最终返回的链表，进行加法运算，逢10加1，加到最短的链表结束为止，判断较长的链表的下一位是否>=10,和如果>=10,它又是否有下一位，没有的话创建新的节点加上去，再加1。

``````/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* p1;
ListNode* p2;
int i =0,count1 = 0,count2 = 0,max=0;
p1 = l1;
p2= l2;
while(p1!=NULL){
count1++;
p1 = p1->next;
}
while(p2!=NULL){
count2++;
p2 = p2->next;
}
p1 =l1;
p2= l2;
if(count1 >= count2){
while(p1 != NULL && p2!= NULL){
p1->val +=p2->val;
if((p1->val) >=10){
if(p1->next!=NULL){
p1->val -=10;
p1->next -> val ++;
}else {
ListNode* newNode = new ListNode(0);
p1->val -=10;
newNode->val = 0;
p1->next = newNode;
p1 = newNode;
p1->val++;
newNode->next = NULL;
}
}
p1 =p1->next;
p2 = p2->next;
}
if(p1!= NULL)
while(p1->val >=10 && p1!= NULL){
if(p1->next!=NULL){
p1->val -=10;
p1->next->val++;
p1 = p1->next;
}else{
ListNode* newNode = new ListNode(0);
p1->val -=10;
newNode->val = 0;
p1->next = newNode;
p1 = newNode;
p1->val++;
newNode->next = NULL;
}

}

return l1;
}else{
while(p1 != NULL && p2!= NULL){
p2->val +=p1->val;
if((p2->val) >=10){
if(p2->next!=NULL){
p2->next -> val ++;
p2->val -=10;
}else{
ListNode* newNode = new ListNode(0);
p2->val -=10;
newNode->val = 0;
p2->next = newNode;
p2 = newNode;
p2->val++;
newNode->next = NULL;
}
}
p1 =p1->next;
p2 = p2->next;
}
if(p2!= NULL)
while(p2->val >=10 && p2!= NULL){
if(p2->next!=NULL){
p2->val -=10;
p2->next->val++;
p2 = p2->next;
}else{
ListNode* newNode = new ListNode(0);
p2->val -=10;
p2->next = newNode;
p2 = newNode;
p2->val++;
newNode->next = NULL;
}
}
return l2;
}
}
};
``````