题目

两个数字求和,数字用链表表示,每一个结点代表一位。链表顺序与数字顺序相反,即表头存放数字的最低位。

解法

分别遍历两个链表的每个结点,对两个结点求和即可。要维护一个变量保存每次相加之后的进位。

更常见的,链表顺序与数字顺序相同,那么做一次链表逆序,求和之后再逆序回来即可。

代码

 /**
* 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 retHead(-), *pr = &retHead, *p1 = NULL, *p2 = NULL;
int carry = ;  //保存每次结点求和之后的进位,用一个bool也可以 for(p1 = l1, p2 = l2; p1 != NULL && p2 != NULL; p1 = p1->next, p2 = p2->next) //p1 p2分别遍历两个链表
{
int sum = p1->val + p2->val + carry;
pr->next = new ListNode(sum % );  //求和结果保存在新结点 pr = pr->next;    //p2指向求和链表的尾结点
carry = sum / ;
} for(ListNode *p = (p1 == NULL ? p2 : p1); p != NULL; p = p->next) //处理两个链表中未处理完的链表剩余节点
{
int sum = p->val + carry;
pr->next = new ListNode(sum % ); pr = pr->next;
carry = sum / ;
} if(carry != )  //如果最后还有进位,必定是1
{
pr->next = new ListNode(carry);
} return retHead.next;
}
};

LeetCode题解——Add Two Numbers的更多相关文章

  1. [LeetCode 题解]: Add Two Numbers

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

  2. leetcode 题解 Add Two Numbers(两个单链表求和)

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

  3. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  4. LeetCode:1. Add Two Numbers

    题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...

  5. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  6. [Leetcode Week15] Add Two Numbers

    Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...

  7. LeetCode 面试:Add Two Numbers

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

  8. LeetCode #002# Add Two Numbers(js描述)

    索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...

  9. [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

    [LeetCode] Add Two Numbers 两个数字相加   You are given two non-empty linked lists representing two non-ne ...

随机推荐

  1. 为什么dubbo使用ZkClient作为zookeeper的客户端

    本文内容并非原创,使用资料均来自互联网. dubbo使用了zkClient而不是使用zookeeper本身的客户端与zookeeper进行交互,为什么呢? 先看看zookeeper本身自带的客户端的问 ...

  2. centos apache 隐藏和伪装 版本信息

    1.隐藏Apache版本信息 测试默认 apache 的状态信息[root@1314it conf]# curl -Is localhostHTTP/1.1 200 OKDate: Tue, 16 N ...

  3. Visual Studio 2013 之 Productivity Power Tools

    http://blogs.msdn.com/b/visualstudio_cn/archive/2014/02/20/visual-studio-2013-productivity-power-too ...

  4. CSS 命名规范及标题供参考与学习

    一.CSS 命名规范   XHTML-CSS写作建议 所有的xhtml代码小写 属性的值一定要用双引号("")括起来,且一定要有值 每个标签都要有开始和结束,且要有正确的层次 空元 ...

  5. PAT-乙级-1001. 害死人不偿命的(3n+1)猜想 (15)

    1001. 害死人不偿命的(3n+1)猜想 (15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 卡拉兹(Ca ...

  6. uva 701

    参考了一下http://hi.baidu.com/renxl51/item/e80b688f9f54aadd5e0ec1de 给一个数字x,求最小的正整数e,使得pow(2,e) == x*pow(1 ...

  7. python检测文件是否更新

    import os import time filename = "test.txt" info = os.stat(filename) if time.time()-info.s ...

  8. Java 向Hbase表插入数据异常org.apache.hadoop.hbase.client.HTablePool$PooledHTable cannot be cast to org.apache.client.HTable

    出错代码如下: //1.create HTablePool HTablePool hp=new HTablePool(con, 1000); //2.get HTable from HTablepoo ...

  9. 【无聊放个模板系列】POJ 1274 (匈牙利)

    #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #inc ...

  10. 【调侃】IOC前世今生 工厂模式 反射 依赖倒置

    http://www.cnblogs.com/showjan/p/3950989.html