【题目】:

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

     Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
     Output: 7 -> 0 -> 8

【题意】:

  首先题意理解,输入为两个单链表,每一个链表的元素均为0~9的数字,而每一个链表示一个数字,头部是最低位,尾部是最高位。例如上述题目中Input的两个链表分别表示342 以及 465.

那么问题就来了,输出为新的链表,该链表以同样的方式表示输入的两个数字之和。

【Key Point】:

  记录进位

【坑】:

  可以写两个方法,将链表内容转换成数字,以及将数字转换成对应的链表。于是就有了以下步骤:

  1. list1, list2   <convertListToNum>  num1, num2;
  2. value = num1 + num2;
  3. value         <convertNumToList>  newList;

  如果题目中规定了链表的长度,这种方法未尝不可,可是没有~  计算机中无论int(最大表示到2^4),long(最大也表示到2^4),long long(最大表示到2^8)等都有极限,而链表理论上可以形成很大的数字,很容易就能够突破 , 所以使用数值类型来存储结果是不可能通过所有case的。因此可以使用非数值型来存储,但是无形中增加了该题目的复杂度。

【解答】:

  既然题目给的链表结果就是从低位向高位(很舒服,如果逆向又得费点事),逐次针对链表两个元素进行相加,记录每一次的进位,注意 题目并没有说输入的两个链表长度一致。以下是代码实现,通过leetCode的OJ

 class Solution {
public:
// 记录每一次的进位,这样就突破了数值型的限制
ListNode *addTwoNumbers2(ListNode *l1, ListNode *l2){
ListNode *root = NULL, *pre = NULL;
int nCarry = ; // 每一个数位能够产生的进位最多为1,所以可以采用bool或者1bit表示 while (NULL != l1 || NULL != l2){
int nodeVal = ;
if (NULL == l1){
nodeVal = l2->val + nCarry;
l2 = l2->next;
}
else if (NULL == l2){
nodeVal = l1->val + nCarry;
l1 = l1->next;
}
else{
nodeVal = l1->val + l2->val + nCarry;
l1 = l1->next;
l2 = l2->next;
} if (nodeVal >= ){ // 产生进位
nCarry = ;
nodeVal %= ;
}
else{
nCarry = ; // 进位清零
} ListNode *node = new ListNode(nodeVal);
if (pre == NULL){
root = node;
}
else{
pre->next = node;
}
pre = node; // 记录上次节点,串联整个链表使用
}// while if (nCarry != ){ // 当链表结束如果还有进位,需要增加一个最高位节点
ListNode * lastNode = new ListNode(nCarry); if (NULL != pre){
pre->next = lastNode;
}
}// nCarry != 0 return root;
}
}

【leetCode Submission】

【运行结果】:

  

  如果有什么问题,希望各位不吝赐教,小弟感恩答谢!

  

[leetCode][016] 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 Week15] Add Two Numbers

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

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

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

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

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

  9. LeetCode之Add Two Numbers

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

随机推荐

  1. HDOJ 1856

    #include<cstdio> #include<cstdlib> typedef struct ufse *ufset; struct ufse { ]; ]; }UFS; ...

  2. Linux统计文件个数

    查看某个文件夹下的文件个数用ls列目录,用grep过虑,再用wc统计即可 用ls -l列出后, 每一行对应一个文件或目录, 如果第一个字母为’-'则为普通文件, 若为’d'则为子目录 + +grep过 ...

  3. 《转载》三年建站之路走得一事无成 今来A5撞墙反思

    本文转载自A5站的蚕丝被.如果给站长带来不便之处,请联系博主. 时间过得真快,记得上一次在A5写文章已经是一年前的事了,这其中是有原因的,今天就跟大家来聊聊三年来个人失败经历的撞墙反思,也给一些有着同 ...

  4. 转数据库Sharding的基本思想和切分策略

    本文着重介绍sharding的基本思想和理论上的切分策略,关于更加细致的实施策略和参考事例请参考我的另一篇博文:数据库分库分表(sharding)系列(一) 拆分实施策略和示例演示 一.基本思想 Sh ...

  5. 解决虚拟机 正在决定eht0 的ip信息失败 无链接-- 添加虚拟网卡

    添加步骤:1.进入设备管理器 2.点下一步3.继续下一步 4.继续往下走

  6. 升级Windows10后Apache服务器启动失败的解决方法

    升级windows10系统后,微软内置了ASP.NET的web高级服务,默认安装了IIS服务器和MSSQL数据库,因为80端口被占用的原因,导致Apache服务器无法正常启动,但是MySQL服务一切正 ...

  7. Java for LeetCode 171 Excel Sheet Column Number

    Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, retur ...

  8. 【JAVA、C++】LeetCode 018 4Sum

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  9. codeforces 479B Towers 解题报告

    题目链接:http://codeforces.com/problemset/problem/479/B 题目意思:有 n 座塔,第 i 座塔有 ai 个cubes在上面.规定每一次操作是从最多 cub ...

  10. LightOJ 1315 - Game of Hyper Knights(博弈sg函数)

    G - Game of Hyper Knights Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & ...