码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 2. Add Two Numbers

时间:2016-10-05 13:15:48      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

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

 

思路:两个链表从第一个节点向后遍历,求两链表对应元素之和。若和大于10,则向下一位进位。最后一个节点,如果存在进位,则需要再向后加一个节点。

 

我的代码如下:(可以通过,但是不够简洁,贴在此处为反面教材)

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
12         
13         ListNode *result=NULL, *temp=NULL;
14         int i=0;//进位
15         
16         if(l1 == NULL && l2 == NULL)
17             return result;
18         else  {
19             result=new ListNode(0);
20             temp=result;
21         }
22         
23         while(l1!=NULL || l2!=NULL)
24         {
25             if(l1!=NULL && l2!=NULL)
26             {
27                 temp->val=(l1->val+l2->val+i)%10;
28                 i=(l1->val+l2->val+i)/10;
29                 l1=l1->next;
30                 l2=l2->next;
31             }
32             else if(l1!=NULL && l2==NULL)
33             {
34                 temp->val=(l1->val+i)%10;
35                 i=(l1->val+i)/10;
36                 l1=l1->next;
37             }
38             else if(l1==NULL && l2!=NULL)
39             {
40                 temp->val=(l2->val+i)%10;
41                 i=(l2->val+i)/10;
42                 l2=l2->next;
43             }
44             
45             if(l1!=NULL || l2!=NULL)
46             {
47                 temp->next=new ListNode(0);
48                 temp=temp->next;
49             }
50 
51         }
52         
53         if(i!=0)//最后一个节点有进位的情况
54         {
55             temp->next=new ListNode(i);
56         }
57         
58         return result;
59     }
60 };

 网上代码如下:(值得学习http://www.jiuzhang.com/solutions/add-two-numbers/

/**
 * 本代码由九章算法编辑提供。没有版权欢迎转发。
 */

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        // 题意可以认为是实现高精度加法
        ListNode *head = new ListNode(0);
        ListNode *ptr = head;
        int carry = 0;
        while (true) {
            if (l1 != NULL) {
                carry += l1->val;
                l1 = l1->next;
            }
            if (l2 != NULL) {
                carry += l2->val;
                l2 = l2->next;
            }
            ptr->val = carry % 10;
            carry /= 10;
            // 当两个表非空或者仍有进位时需要继续运算,否则退出循环
            if (l1 != NULL || l2 != NULL || carry != 0) {
                ptr = (ptr->next = new ListNode(0));
            } else break;
        }
        return head;
    }
};

 

LeetCode 2. Add Two Numbers

标签:

原文地址:http://www.cnblogs.com/gexinmen/p/5932028.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!