LeetCode Add Two Numbers

来源:互联网 发布:微软access数据库 编辑:程序博客网 时间:2024/06/05 01:10
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

关于这一题,主要是解决单链表的问题,如果你对单链表的基本操作比较熟悉的话,这个题目还是比较简单的,首先简单的讲一下我做题的思路:

1.单链表的每一个节点的val都是一个整数相应位的权重值,而且是相应权重位的逆序。

2.先将l1和l2当前的val以及进位位相加并判断此值是否大于9,当大于时进行对10求余再赋值给l1的val,同时设置进位位为1。

3.需要注意的是当最高位以及进位位相加后大于9时,还需要再新建一个节点,以便存储。


下面是我原先的详细的源代码:

class Solution {public:    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {        ListNode *cur = NULL , *prev = NULL , *result = NULL;        int carry = 0;        while(carry || l1 || l2)            {                int temp = 0;                if(l1&&l2)                 {                     temp = l1->val + l2->val + carry;                        carry = 0;                        if(temp>=10)                            {                                carry = 1;                                temp-=10;                            }                        cur = new ListNode(temp);                        l1 = l1->next;l2 = l2->next;                        if(NULL == prev)                            {prev = cur;                                result = prev;                            }                            else{                                prev->next = cur;                                prev = prev->next;                            }                }else if(l1)                    {                         temp = l1->val + carry;                        carry = 0;                        if(temp>=10)                            {                                carry = 1;                                temp-=10;                            }                        cur = new ListNode(temp);                        l1 = l1->next;                         if(NULL == prev)                            {prev = cur;                            result = prev;                            }                            else{                                prev->next = cur;                                prev = prev->next;                            }                                            }else if(l2)                        {                          temp = l2->val + carry;                        carry = 0;                        if(temp>=10)                            {                                carry = 1;                                temp-=10;                            }                        cur = new ListNode(temp);                            l2 = l2->next;                         if(NULL == prev)                            {prev = cur;                            result = prev;                            }                            else{                                prev->next = cur;                                prev = prev->next;                            }                         }else{                            prev->next = new ListNode(carry);                        }            }        return result;    }};


这样的代码在LeetCode中是通不过了,它会提示:Memory Limit Exceeded

下面是修改过的代码:

class Solution {public:    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {            struct ListNode *cur = NULL,*prev = NULL, *result = NULL;            int carry = 0;            while(l1 || l2|| carry)                {                    int value = carry;                    if(l1)                        {                            value+=l1->val;                            l1 = l1->next;                        }                    if(l2)                        {                            value = l2->val + value;                            l2 = l2->next;                        }                        carry = 0;                    if(value>=10)                        {                            carry = 1;                            value-=10;                        }                    cur = new ListNode(value);                    if(prev == NULL)                        {                            prev = cur;                            result = cur;                        }else{                            prev->next = cur;                            prev = cur;                        }                }            return result;    }};



0 0
原创粉丝点击