【题目】:

    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. C#关键字base

    例子: public CustomStroke(SharpType type) :base() { this.type = type; } 这里的CustomStroke继承与基类Stroke类,用关 ...

  2. BestCoder Round #60 1002

    Problem Description You are given two numbers NNN and MMM. Every step you can get a new NNN in the w ...

  3. HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)

    C - 最大连续子序列 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  4. 【Python】Django 如何直接返回404 被 curl,wget 捕获到

    代码示例: from django.http import Http404, HttpResponseNotFound #raise Http404(filename) return HttpResp ...

  5. HDU 5745 La Vie en rose (DP||模拟) 2016杭电多校联合第二场

    题目:传送门. 这是一道阅读理解题,正解是DP,实际上模拟就能做.pij+1 指的是 (pij)+1不是 pi(j+1),判断能否交换输出即可. #include <iostream> # ...

  6. [Android Pro] Android签名与认证详细分析之二(CERT.RSA剖析)

    转载自: http://www.thinksaas.cn/group/topic/335449/ http://blog.csdn.net/u010571535/article/details/899 ...

  7. fedora yum 使用代理的方法

    配置yum: 编辑/etc/yum.conf添加下列一行: proxy=http://domain/user:passwd@<proxy ip>:80 <proxy ip>:代 ...

  8. jquery.validate.unobtrusive.js插件作用

    在 ASP.NET MVC 中启用 Unobtrusive JavaScript 功能,可以在运行时由服务器端根据Model中设置的验证规则,自动生成客户端验证js代码(结合jquery.valida ...

  9. CI如何接受POST请求中的JSON数据

    PHP默认只识别application/x-www.form-urlencoded标准的数据类型 “php://input可以读取没有处理过的POST数据.相较于$HTTP_RAW_POST_DATA ...

  10. (转)云存储:阿里云OSS 、又拍云和 七牛 的比较

    阿里OSS:好处就是,那是一套完整的体系,存储,数据库,CDN,服务器,阿里都可以给你全包.缺点,费用对于没有盈利的网站来说太高了,好像定位就是给那些高端客户使用的,而且CDN,OSS的流量是分开收费 ...