给定两个非空链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储单个数字。将这两数相加会返回一个新的链表。
你可以假设除了数字 0 之外,这两个数字都不会以零开头。
进阶:
如果输入链表不能修改该如何处理?换句话说,你不能对列表中的节点进行翻转。
示例:
输入: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
输出: 7 -> 8 -> 0 -> 7
C
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
int getlen(struct ListNode* head)
{
int count=0;
while(head)
{
count++;
head=head->next;
}
return count;
}
struct ListNode* reversed(struct ListNode* head)
{
if(NULL==head || NULL==head->next)
{
return head;
}
struct ListNode* pcur=head;
struct ListNode* pnew=NULL;
struct ListNode* pnext=NULL;
while(pcur)
{
pnext=pcur->next;
pcur->next=pnew;
pnew=pcur;
pcur=pnext;
}
return pnew;
}
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
l1=reversed(l1);
l2=reversed(l2);
int count1=getlen(l1);
int count2=getlen(l2);
struct ListNode* large;
struct ListNode* small;
if(count1>count2)
{
large=l1;
small=l2;
}
else
{
large=l2;
small=l1;
}
struct ListNode* p=(struct ListNode*)malloc(sizeof(struct ListNode));
p->next=NULL;
int add=0;
int n=small->val+large->val+add;
if(n<10)
{
p->val=n;
add=0;
}
else
{
p->val=n%10;
add=n/10;
}
small=small->next;
large=large->next;
struct ListNode* head=p;
while(small)
{
p->next=(struct ListNode*)malloc(sizeof(struct ListNode));
p->next->next=NULL;
int num=small->val+large->val+add;
if(num<10)
{
p->next->val=num;
p=p->next;
add=0;
}
else
{
p->next->val=num%10;
p=p->next;
add=num/10;
}
small=small->next;
large=large->next;
}
while(large)
{
int sum=add+large->val;
p->next=(struct ListNode*)malloc(sizeof(struct ListNode));
p->next->next=NULL;
if(sum<10)
{
p->next->val=sum;
add=0;
p=p->next;
}
else
{
p->next->val=sum%10;
add=sum/10;
p=p->next;
}
large=large->next;
}
if(add)
{
p->next=(struct ListNode*)malloc(sizeof(struct ListNode));
p->next->next=NULL;
p->next->val=add;
}
return reversed(head);
}
C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverse(ListNode* head)
{
if(NULL==head || NULL==head->next)
{
return head;
}
ListNode* pnew=NULL;
ListNode* pnext=NULL;
ListNode* pcur=head;
while(pcur)
{
pnext=pcur->next;
pcur->next=pnew;
pnew=pcur;
pcur=pnext;
}
return pnew;
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
{
l1=reverse(l1);
l2=reverse(l2);
int m=0;
int n=0;
ListNode* p=l1;
while(p)
{
m++;
p=p->next;
}
p=l2;
while(p)
{
n++;
p=p->next;
}
ListNode* large=NULL;
ListNode* small=NULL;
if(m>n)
{
large=l1;
small=l2;
}
else
{
large=l2;
small=l1;
}
ListNode* pnew=new ListNode(-1);
p=pnew;
int mod=0;
while(small)
{
int val=large->val+small->val+mod;
p->next=new ListNode(val%10);
mod=val/10;
large=large->next;
small=small->next;
p=p->next;
}
while(large)
{
int val=large->val+mod;
p->next=new ListNode(val%10);
mod=val/10;
large=large->next;
p=p->next;
}
if(mod)
{
p->next=new ListNode(mod);
}
pnew=pnew->next;
return reverse(pnew);
}
};
python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def getlen(self, head):
count=0
while head:
count+=1
head=head.next
return count
def rev(self, head):
if None==head or None==head.next:
return head
pcur=head
pnew=None
pnext=None
while pcur:
pnext=pcur.next
pcur.next=pnew
pnew=pcur
pcur=pnext
return pnew
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
l1=self.rev(l1)
l2=self.rev(l2)
count1=self.getlen(l1)
count2=self.getlen(l2)
large=None
small=None
if count1>count2:
large=l1
small=l2
else:
large=l2
small=l1
p=None
ad=0
num=large.val+small.val+ad
if num<10:
p=ListNode(num)
ad=0
else:
p=ListNode(num%10)
ad=num//10
large=large.next
small=small.next
head=p
#p=p.next
while small:
num=small.val+large.val+ad
if num<10:
p.next=ListNode(num)
ad=0
else:
p.next=ListNode(num%10)
ad=num//10
p=p.next
small=small.next
large=large.next
while large:
num=large.val+ad
if num<10:
p.next=ListNode(num)
ad=0
else:
p.next=ListNode(num%10)
ad=num//10
p=p.next
large=large.next
if ad:
p.next=ListNode(ad)
return self.rev(head)