【LeetCode】【Python】【C++】2. Add Two Numbers代码实现

来源:互联网 发布:淘宝apass会员资格 编辑:程序博客网 时间:2024/06/05 07:38

题目:

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)

Output: 7 -> 0 -> 8


思路:

python中可以将两个链表转化为整型后进行相加,最后构造相加结果的单链表返回即可。
但是C++中因为数据溢出并不能这样实现。显然出题者意图并不是让我们按以上思路求解。

python实现:

# Definition for singly-linked list.class ListNode(object):    def __init__(self, x):        self.val = x        self.next = Noneclass Solution(object):    def addTwoNumbers(self, l1, l2):        """        :type l1: ListNode        :type l2: ListNode        :rtype: ListNode        """        listnum1=[]        listnum2=[]        while l1 !=None:            listnum1.append(l1.val)            l1=l1.next        while l2 !=None:            listnum2.append(l2.val)            l2=l2.next        num1=0        num2=0        for i in range(len(listnum1)):            num1=num1+listnum1[i]*(10**i)        for i in range(len(listnum2)):             num2 = num2 + listnum2[i] * (10 ** i)        sum=num1+num2        l3=ListNode(0)        p = ListNode(0)        p=l3        while sum >=10:            tem=ListNode(None)            p.val=sum%10            p.next=tem            p=tem            sum=sum//10        p.val=sum        return l3


C++实现:


#include#includeusing namespace std;//Definition for singly-linked list.struct ListNode {    int val;    ListNode *next;    ListNode(int x) : val(x), next(NULL) {}};class Solution {public:__int64 trans(ListNode* l1){__int64 a = 0;int i=0;while (l1 != NULL){a = l1->val * pow(10,i)+a;l1 = l1->next;i++;}return a;}ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {__int64 a = trans(l1);__int64 b = trans(l2);__int64 c = a + b;ListNode* begin, *end, *p;begin = new ListNode(c % 10);end = begin;c = c / 10;while (c >= 1){p = new ListNode(c%10);end->next = p;end = p;c = c / 10;}return begin;}};int main(){Solution A;ListNode* l1, *l11, *l2, *l22,*p;l1 = new ListNode(1);l11 = l1;p = new ListNode(9);l11->next = p;l11 = p;p = new ListNode(9);l11->next = p;l11 = p;p = new ListNode(9);l11->next = p;l11 = p;p = new ListNode(9);l11->next = p;l11 = p;p = new ListNode(9);l11->next = p;l11 = p;p = new ListNode(9);l11->next = p;l11 = p;p = new ListNode(9);l11->next = p;l11 = p;p = new ListNode(9);l11->next = p;l11 = p;p = new ListNode(9);l11->next = p;l11 = p;l2 = new ListNode(9);l22 = l2;ListNode* result;result=A.addTwoNumbers(l1,l2);cout << "";return 0;}



然而这样数据大的时候会出现溢出,于是下面是一种可接受的方法,思路是:对每一个链表元素分别相加,如有进位则设flag为1。
class Solution {public:ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {ListNode*l3,*l33;l3 = new ListNode(0);l33 = l3;int flag = 0;while (l1!=NULL||l2!=NULL){if (l1 == NULL){ListNode* p;p = new ListNode((l2->val+flag)%10);l33->next = p;l33 = p;flag = (l2->val + flag) / 10;l2 = l2->next;}else if (l2 == NULL){ListNode* p;p = new ListNode((l1->val+flag)%10);l33->next = p;l33 = p;flag = (l1->val + flag) / 10;l1 = l1->next;}else{ListNode* p;p = new ListNode((l1->val + l2->val + flag)%10);l33->next = p;l33 = p;flag = (l1->val + l2->val+flag) / 10;l1 = l1->next;l2 = l2->next;}}if (flag == 1){ListNode* p;p = new ListNode(flag);l33->next = p;l33 = p;}l3 = l3->next;return l3;}};


0 0
原创粉丝点击