LeetCode 445. 兩數相加 II(C、C++、python)
阿新 • • 發佈:2018-12-16
給定兩個非空連結串列來代表兩個非負整數。數字最高位位於連結串列開始位置。它們的每個節點只儲存單個數字。將這兩數相加會返回一個新的連結串列。
你可以假設除了數字 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* pnext=NULL; ListNode* pnew=NULL; ListNode* pcur=head; while(pcur) { pnext=pcur->next; pcur->next=pnew; pnew=pcur; pcur=pnext; } return pnew; } int getlen(ListNode* head) { int count=0; while(head) { count++; head=head->next; } return count; } ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { l1=reverse(l1); l2=reverse(l2); int count1=getlen(l1); int count2=getlen(l2); ListNode* large; ListNode* small; if(count1>count2) { large=l1; small=l2; } else { large=l2; small=l1; } int add=0; int n=large->val+small->val+add; ListNode* p; if(n<10) { p=new ListNode(n); } else { p=new ListNode(n%10); add=n/10; } small=small->next; large=large->next; ListNode* head=p; while(small) { int num=small->val+large->val+add; if(num<10) { p->next=new ListNode(num); add=0; p=p->next; } else { p->next=new ListNode(num%10); add=num/10; p=p->next; } small=small->next; large=large->next; } while(large) { int sum=add+large->val; if(sum<10) { p->next=new ListNode(sum); add=0; p=p->next; } else { p->next=new ListNode(sum%10); add=sum/10; p=p->next; } large=large->next; } if(add) { p->next=new ListNode(add); } return reverse(head); } };
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)