题目

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

解法

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

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

代码

 /**
* 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. python读取mnist

    python读取mnist 其实就是python怎么读取binnary file mnist的结构如下,选取train-images TRAINING SET IMAGE FILE (train-im ...

  2. ASP + ACCESS 上传图片到数据库与将图片读出数据库显示之实现

    1.uppic.asp:上传图片程序 <% dim rs dim formsize,formdata,bncrlf,divider,datastart,dataend,mydata formsi ...

  3. [BEC][hujiang] Lesson04 Unit1:Working life ---Reading + Listening &Grammar & Speaking

    4 1.1 Working life    P10 Reading----The anonymous CV Exercise 3  What should be included in the CV ...

  4. hdu 2147 kiki's game 博弈论

    找规律的博弈论!! 很容易发现当n,m都为奇数时先手必败! 代码如下: #include<iostream> #include<stdio.h> #define I(x,y) ...

  5. zoj 3640 Help Me Escape 概率DP

    记忆化搜索+概率DP 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include ...

  6. js 中中括号,大括号使用详解

    一.{ } 大括号,表示定义一个对象,大部分情况下要有成对的属性和值,或是函数.如:var LangShen = {"Name":"Langshen",&quo ...

  7. 使用头文件climits中的符号常量获知整型数据的表数范围---gyy整理

    在头文件climits(limits.h)以宏定义的方式定义了各种符号常量来表示各种整型类型表示数的范围,如int的最大最小值,long的最大最小值等. 符号常量 表示 CHAR_BIT char 的 ...

  8. 198. House Robber

    题目: You are a professional robber planning to rob houses along a street. Each house has a certain am ...

  9. 【剑指offer】数字在排序数组中出现的次数

    2013-09-02 16:28:35 找出数字在排序数组中出现的次数. 注意几点: 一开始试图用size_t类型表示数组的下标begin.end,到那时这样做在end = 0时,end - 1是si ...

  10. redolog 大小的实验

    前言:近日因工作需要,测试postgresql和MySQL在oltp对比测试,因结果差异太多(MySQL测试结果比较差,相同环境),寻求大神帮助,有幸得叶大师和姜大师指点,指出my.cnf配置文件in ...