zoukankan      html  css  js  c++  java
  • 167 链表求和

    原题网址:http://www.lintcode.com/zh-cn/problem/add-two-numbers/#

    你有两个用链表代表的整数,其中每个节点包含一个数字。数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头。写出一个函数将两个整数相加,用链表形式返回和。

    样例

    给出两个链表 3->1->5->null 和 5->9->2->null,返回 8->0->8->null

    标签 
     
    尤其注意进位问题,以及访问链表节点时注意其是否为NULL。
      1 /**
      2  * Definition of singly-linked-list:
      3  * class ListNode {
      4  * public:
      5  *     int val;
      6  *     ListNode *next;
      7  *     ListNode(int val) {
      8  *        this->val = val;
      9  *        this->next = NULL;
     10  *     }
     11  * }
     12  */
     13 
     14 class Solution {
     15 public:
     16     /**
     17      * @param l1: the first list
     18      * @param l2: the second list
     19      * @return: the sum list of l1 and l2 
     20      */
     21     ListNode * addLists(ListNode * l1, ListNode * l2) {
     22         // write your code here
     23         if (l1==NULL&&l2==NULL)
     24         {
     25             return NULL;
     26         }
     27         if (l1==NULL)
     28         {
     29             return l2;
     30         }
     31         if (l2==NULL)
     32         {
     33             return l1;
     34         }
     35 
     36         ListNode *temp1=l1;
     37         ListNode *temp2=l2;
     38         
     39         ListNode *sum=new ListNode(0);
     40         ListNode *result=sum;
     41 
     42         while(temp1->next!=NULL&&temp2->next!=NULL)
     43         {
     44             int tempsum=temp1->val+temp2->val;
     45             if (tempsum>9)
     46             {
     47                 tempsum=tempsum%10;
     48                 temp1->next->val=temp1->next->val+1; //进位;
     49             }
     50             ListNode *tempSum=new ListNode(tempsum);
     51             sum->next=tempSum;
     52             sum=tempSum;  //索引指针移动到尾结点,以便下次添加新节点;
     53             temp1=temp1->next;
     54             temp2=temp2->next;
     55         }
     56 
     57         //相等;
     58         if (temp1->next==NULL&&temp2->next==NULL)
     59         {
     60             int tempsum=temp1->val+temp2->val;
     61             if (tempsum>9)
     62             {
     63                 tempsum=tempsum%10;
     64                 ListNode *tempSum=new ListNode(tempsum); //此处尤其注意要添加的是两个结点;
     65                 sum->next=tempSum;
     66                 sum=tempSum;
     67                 ListNode *tempSumm=new ListNode(1);
     68                 sum->next=tempSumm;
     69                 return result->next;
     70             }
     71             else
     72             {
     73                 ListNode *tempSum=new ListNode(tempsum);
     74                 sum->next=tempSum;
     75                 return result->next;
     76             }
     77         }
     78 
     79         //l1节点数多于l2;
     80         if (temp1->next!=NULL)
     81         {
     82             int tempsum=temp1->val+temp2->val;
     83             if (tempsum>9)
     84             {
     85                 tempsum=tempsum%10;
     86                 temp1->next->val++;
     87             }
     88             ListNode *tempSum=new ListNode(tempsum);
     89             sum->next=tempSum;
     90             sum=tempSum;
     91             temp1=temp1->next;
     92 
     93             while(temp1!=NULL)
     94             {
     95                 int tempsum=temp1->val;
     96                 if (tempsum>9)
     97                 {
     98                     tempsum=tempsum%10;
     99                     if (temp1->next!=NULL)
    100                     {
    101                         temp1->next->val++;
    102                     }
    103                     else
    104                     {
    105                         ListNode *tempSum=new ListNode(tempsum);
    106                         sum->next=tempSum;
    107                         sum=tempSum;
    108                         ListNode *tempSumm=new ListNode(1);
    109                         sum->next=tempSumm;
    110                         return result->next;
    111                     }
    112                 }
    113                 ListNode *tempSum=new ListNode(tempsum);
    114                 sum->next=tempSum;
    115                 sum=tempSum;
    116                 temp1=temp1->next;
    117             }
    118         }
    119         //l2多于l1;
    120         if (temp2->next!=NULL)
    121         {
    122             int tempsum=temp1->val+temp2->val;
    123             if (tempsum>9)
    124             {
    125                 tempsum=tempsum%10;
    126                 temp2->next->val++;
    127             }
    128             ListNode *tempSum=new ListNode(tempsum);
    129             sum->next=tempSum;
    130             sum=tempSum;
    131             temp2=temp2->next;
    132 
    133             while(temp2!=NULL)
    134             {
    135                 int tempsum=temp2->val;
    136                 if (tempsum>9)
    137                 {
    138                     tempsum=tempsum%10;
    139                     if (temp2->next!=NULL)
    140                     {
    141                         temp2->next->val++;
    142                     }
    143                     else
    144                     {
    145                         ListNode *tempSum=new ListNode(tempsum);
    146                         sum->next=tempSum;
    147                         sum=tempSum;
    148                         ListNode *tempSumm=new ListNode(1);
    149                         sum->next=tempSumm;
    150                         return result->next;
    151                     }
    152                 }
    153                 ListNode *tempSum=new ListNode(tempsum);
    154                 sum->next=tempSum;
    155                 sum=tempSum;
    156                 temp2=temp2->next;
    157             }
    158         }
    159         
    160         return result->next;
    161     }
    162 };

    代码的优化:

    https://blog.csdn.net/lyy_hit/article/details/49787439

  • 相关阅读:
    .NET 第一天
    C# 多线程操作同一文件
    c# 进制转换-续
    C# 进制转化
    DevExpress.Utils.ToolTipLocation
    gridView 练习
    dashboard 数据绑定的时候 addTable 是视图
    gridLookUpEdit1
    gridview1 设置 内容居中 标题剧中
    LOOKupE
  • 原文地址:https://www.cnblogs.com/Tang-tangt/p/8668027.html
Copyright © 2011-2022 走看看