Add Two Numbers 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/add-two-numbers/description/


Description

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.

Solution

class Solution {
private:
ListNode* copyList(ListNode* listHeadNode) {
if (listHeadNode == NULL)
return NULL;
ListNode* resHeadNode = new ListNode(listHeadNode -> val);
ListNode* resCurNode = resHeadNode;
ListNode* listCurNode = listHeadNode -> next;
while (listCurNode != NULL) {
resCurNode -> next = new ListNode(listCurNode -> val);
resCurNode = resCurNode -> next;
listCurNode = listCurNode -> next;
}
return resHeadNode;
}
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
if (l1 == NULL)
return copyList(l2);
if (l2 == NULL)
return copyList(l1);
bool isOverflow = false;
int curVal = l1 -> val + l2 -> val;
if (curVal >= 10) {
isOverflow = true;
}
ListNode* resHeadNode = new ListNode(curVal % 10);
ListNode* resPreNode = resHeadNode;
ListNode* l1CurNode = l1 -> next;
ListNode* l2CurNode = l2 -> next;
while (l1CurNode != NULL && l2CurNode != NULL) {
curVal = l1CurNode -> val + l2CurNode -> val;
if (isOverflow)
curVal++;
resPreNode -> next = new ListNode(curVal % 10);
isOverflow = (curVal / 10 > 0); resPreNode = resPreNode -> next;
l1CurNode = l1CurNode -> next;
l2CurNode = l2CurNode -> next;
}
if (l1CurNode == NULL)
resPreNode -> next = copyList(l2CurNode);
else
resPreNode -> next = copyList(l1CurNode); if (isOverflow) {
if (resPreNode -> next == NULL) {
resPreNode -> next = new ListNode(1);
} else {
while (true) {
if (resPreNode -> next == NULL) {
resPreNode -> next = new ListNode(1);
break;
} else {
curVal = resPreNode -> next -> val + 1;
if (curVal >= 10) {
resPreNode -> next -> val = curVal % 10;
resPreNode = resPreNode -> next;
} else {
resPreNode -> next -> val = curVal;
break;
}
}
}
}
}
return resHeadNode;
}
};

解题描述

这道题可以说解决的是数字相加的问题,但是由于数字的长度不确定,输入的数字可能是现有的内置数据类型无法存储的,所以像题目中给出的方法是,利用链表来记录数字,然后针对链表中的每一个节点的值进行相加。

明确了题目的要求之后,很快就可以清楚,题目最需要解决的问题就是链表边界和相加进位的问题。我的做法算是简单的对特殊情况打补丁。

[Leetcode Week15] Add Two Numbers的更多相关文章

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

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

  2. LeetCode:1. Add Two Numbers

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

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

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

  4. LeetCode 面试:Add Two Numbers

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

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

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

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

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

  7. [LeetCode] 2. Add Two Numbers 两个数字相加

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

  8. LeetCode之Add Two Numbers

    Add Two Numbers 方法一: 考虑到有进位的问题,首先想到的思路是: 先分位求总和得到 totalsum,然后再将totalsum按位拆分转成链表: ListNode* addTwoNum ...

  9. LeetCode 2. add two numbers && 单链表

    add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...

随机推荐

  1. Android基础------高级ul:消息提示

    前言:Android消息提示笔记,刚刚接触Android 1.静态方法Toast 直接调用静态方法 //消息提示(context,"内容",固定时间) Toast.makeText ...

  2. BZOJ4300 绝世好题(动态规划)

    设f[i][j]为前i个数中所选择的最后一个数在第j位上为1时的最长序列长度,转移显然. #include<iostream> #include<cstdio> #includ ...

  3. BZOJ4260 Codechef REBXOR(trie)

    用trie求出前缀最大区间异或和.后缀最大区间异或和即可.注意空间是nlog的. #include<iostream> #include<cstdio> #include< ...

  4. POJ2774:Long Long Message——题解

    http://poj.org/problem?id=2774 给定两个字符串 A 和 B,求最长公共子串. 论文题,把两个串合并起来,比较两个串各自的后缀的height值取最大即可. #include ...

  5. MySQL安装出现“不是内部或外部命令,也不是可执行程序”等一系列问题的解决方案

    MySQL安装出现“不是内部或外部命令,也不是可执行程序” 一.这是应该是环境变量处问题了,设置如下: 1)右击我的电脑选择“属性”,找到“高级系统设置” 2)在系统属性下,选择“高级”中的“环境变量 ...

  6. HDOJ(HDU).2044-2049 递推专题

    HDOJ(HDU).2044-2049 递推专题 点我挑战题目 HDU.2044 题意分析 先考虑递推关系:从1到第n个格子的时候由多少种走法? 如图,当n为下方格子的时候,由于只能向右走,所以有2中 ...

  7. PLAN OF HEOI(unfinished)

    Au:整体二分/计算几何/多项式/fwtAg:可持久化重量平衡树/线段树分治/线段树合并/最短路树/最短路DAGCu:三分Up:博弈论/置换群/杜教筛/矩阵树定理/BSGS/动态树分治/网络流(线性规 ...

  8. Xcode 问题

    问题: 昨天在写代码的时候,不知道修改了哪个地方,Xcode6突然犯病了,在当前项目下无法代码提示,但是在新建工程中没有任何问题,其中重装了Xcode6也没有把问题解决, 最终的解决办法是: 在fin ...

  9. 如何通过反射来创建对象?getConstructor()和getDeclaredConstructor()区别?

    1. 通过类对象调用newInstance()方法,适用于无参构造方法: 例如:String.class.newInstance() public class Solution { public st ...

  10. Service学习

    一.采用startService方式开启服务 1.写一个服务类 public class PhoneService extends Service { private static final Str ...