【转载请注明】http://www.cnblogs.com/igoslly/p/8672467.html

来看一下题目:

You are given two non-empty linked lists representing two non-negative integers. 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.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

题目意思其实就是将两个数,以链表逆序的方式进行存储

求和后,将结果也逆序输出

思路:

1、由于两个数的大小位数,链表 -> int数进行的想法基本无望,可能越界

2、链表已经逆序,已经提供很好的“对应位相加,向前进位”的运算模式

注意点:

1、List1和List2不等长

2、当某个List完成后,可单个计算另外的List

3、考虑进位关系,例如9+99999

4、最后进位时,需额外设定val=1的结点


实现方法1(初始):

方法1即是依照上面思路,依样画葫芦得到的结果

但其实细看,本题代码重复的部分太多,而且一直需要使用pre变量跟踪前结点,有些多余

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *p1=l1,*p2=l2,*pre=p1;
int up=; // 将结点l1作为结果链表返回,在l1和l2均在有效范围内,进行添加,同时更新up进位
while(p1&&p2){
p1->val+=p2->val+up;
up=p1->val/;
if(up==){
p1->val-=;
}
pre=p1;
p1=p1->next;
p2=p2->next;
} // 当l1结束后,pre最后个元素连接到l2后部
if(p1==NULL){
pre->next=p2;
while(p2!=NULL){
p2->val+=up;
up=p2->val/;
if(up==){
p2->val-=;
}
pre=p2;
p2=p2->next;
}
} // 当l2结束后,单独计算l1
if(p2==NULL){
while(p1!=NULL){
p1->val+=up;
up=p1->val/;
if(up==){
p1->val-=;
}
pre=p1;
p1=p1->next;
}
} // 当计算结束,up=1时,表示和多一位,新创建val=1的ListNode添加
if(up==){
pre->next=new ListNode();
}
return l1;
}
};

实现方法2 (对方法1进行优化):

相对看起来更加简洁

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int carry=;
ListNode* listNode=new ListNode();
ListNode* p1=l1,*p2=l2,*p3=listNode; // 修改判断条件从 && 到 ||
while(p1!=NULL||p2!=NULL)
{
// 在while循环里添加p1和p2的判断,省去了某个List完毕后单独List的情况
if(p1!=NULL)
{
carry+=p1->val;
p1=p1->next;
}
if(p2!=NULL)
{
carry+=p2->val;
p2=p2->next;
}
p3->next=new ListNode(carry%);
p3=p3->next;
carry/=;
} // 由于辟出了单独的result链表,故而无需再用pre继续前结点
if(carry==)
p3->next=new ListNode();
return listNode->next;
}
};

Leetcode0002--Add Two Numbers 链表求和的更多相关文章

  1. LeetCode-2. Add Two Numbers(链表实现数字相加)

    1.题目描述 You are given two non-empty linked lists representing two non-negative integers. The digits a ...

  2. [LeetCode] Add Two Numbers 链表

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  3. 【LeetCode】2.Add Two Numbers 链表数相加

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  4. 002 Add Two Numbers 链表上的两数相加

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  5. Add Two Numbers(链表)

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  6. leetcode 2 Add Two Numbers(链表)

    数字反过来这个没有什么麻烦,就是镜像的去算十进制加法就可以了,然后就是简单的链表. /** * Definition for singly-linked list. * struct ListNode ...

  7. [LeetCode]2. Add Two Numbers链表相加

    注意进位的处理和节点为null的处理 public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int flag = 0; ListNode ...

  8. 链表求和12 · Add Two Numbers

    反向存储,从左往右加 [抄题]: 你有两个用链表代表的整数,其中每个节点包含一个数字.数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头.写出一个函数将两个整数相加,用链表形式返回和.给 ...

  9. LeetCode 445. Add Two Numbers II(链表求和)

    题意:两个非空链表求和,这两个链表所表示的数字没有前导零,要求不能修改原链表,如反转链表. 分析:用stack分别存两个链表的数字,然后从低位开始边求和边重新构造链表. Input: (7 -> ...

随机推荐

  1. 新版本的molar mass(uva-1586)明明debug过了,各种测试还是WA真是气死我了

    #include <bits/stdc++.h> using namespace std; double trans(string a) { stringstream ss; ss< ...

  2. Cashier (codeforces 1059A)

    题目倒是不难注意第一个时间段可能不是从零开始的,所以注意第一个时间的开始节点与零之间可能存在休息的时间 还有这个题我打的时候一直谜之RE......发现原来bool函数忘记写return了.....以 ...

  3. hdu2001 计算两点间的距离【C++】

    计算两点间的距离 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  4. (39.4) Spring Boot Shiro权限管理【从零开始学Spring Boot】

    在读此文章之前您还可能需要先了解: (39.1) Spring Boot Shiro权限管理[从零开始学Spring Boot] http://412887952-qq-com.iteye.com/b ...

  5. [cf 599A]Patrick and Shopping

    傻逼题,但是我还是wa了一发. #include <iostream> using namespace std; int main() { long long a,b,c,Ans=0x7f ...

  6. 树网的核(codevs 1167)

    题目描述 Description [问题描述]设 T=(V, E, W) 是一个无圈且连通的无向图(也称为无根树),每条边带有正整数的权,我们称T 为树网(treenetwork),其中V, E分别表 ...

  7. java 垃圾收集

    1.为什么使用垃圾收集 a.把用户从释放占用内存的重担中解救出来 b.帮助程序保持完整性 2.垃圾收集算法 检测出垃圾对象,必须回收垃圾对象所使用的堆空间并还给程序 垃圾检测:通过建立一个根对象集合并 ...

  8. POJ 3304 segments 线段和直线相交

    Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14178   Accepted: 4521 Descrip ...

  9. kendo Grid的toolbar自定义

    由于这个toolbar官方进增加了create,save还有一个是_____ 所以想要自定义话就需要使用 下面的代码(这个是MVVM模式) data-toolbar='[{ template: Ken ...

  10. [bzoj1704][Usaco2007 Mar]Face The Right Way 自动转身机_贪心

    Face The Right Way 自动转身机 bzoj-1704 Usaco-2007 Mar 题目大意:不想描述题意系列++... ...题目链接 注释:略. 想法:我们直接枚举k,然后从左往右 ...