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

首先使用链表实现等长无进位的求和,即实现 (1 —> 2 -> 3) + (1 -> 2 -> 3)=(2 -> 3 -> 6)

 #include <iostream>

 using namespace std;

 struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { int val1 = , val2 = ; ListNode *l = NULL;
ListNode *l_end = NULL;
ListNode *l_new = NULL; val1 = l1->val;
val2 = l2->val;
l1 = l1->next;
l2 = l2->next;
l_new = (ListNode *)new ListNode((val1 + val2) % );
l_new->next = NULL;
l = l_new;
l_end = l;
while (l1 != NULL || l2 != NULL){ val1 = l1->val;
val2 = l2->val;
l1 = l1->next;
l2 = l2->next; l_new = (ListNode *)new ListNode((val1 + val2)%);
l_new->next = NULL; l_end->next = l_new;
l_end = l_new;
} return l; }
}; int main()
{
ListNode *l1, *l2;
ListNode *l;
l1 = (ListNode *)new ListNode();
l2 = (ListNode *)new ListNode();
l1->next = (ListNode *)new ListNode();
l2->next = (ListNode *)new ListNode();
l1->next->next = (ListNode *)new ListNode();
l2->next->next = (ListNode *)new ListNode();
Solution s;
l = s.addTwoNumbers(l1, l2);
while (l != NULL){
cout << l->val << endl;
l = l->next;
}
while ();
}

运行结果:

然后实现不等长无进位的求和,即实现 (1 —> 2 -> 3) + (1)=(2 -> 2 -> 3)

 #include <iostream>

 using namespace std;

 struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { int val1 = , val2 = ; ListNode *l = NULL;
ListNode *l_end = NULL;
ListNode *l_new = NULL; val1 = l1->val;
val2 = l2->val;
l1 = l1->next;
l2 = l2->next;
l_new = (ListNode *)new ListNode((val1 + val2) % );
l_new->next = NULL;
l = l_new;
l_end = l;
while (l1 != NULL || l2 != NULL){
if (l1 == NULL){
val2 = l2->val;
l2 = l2->next;
val1 = ;
}
else if (l2 == NULL){
val1 = l1->val;
l1 = l1->next;
val2 = ;
}
else{
val1 = l1->val;
val2 = l2->val;
l1 = l1->next;
l2 = l2->next;
} l_new = (ListNode *)new ListNode((val1 + val2)%);
l_new->next = NULL; l_end->next = l_new;
l_end = l_new;
} return l; }
}; int main()
{
ListNode *l1, *l2;
ListNode *l;
l1 = (ListNode *)new ListNode();
l2 = (ListNode *)new ListNode();
l1->next = (ListNode *)new ListNode();
//l2->next = (ListNode *)new ListNode(2);
l1->next->next = (ListNode *)new ListNode();
//l2->next->next = (ListNode *)new ListNode(3);
Solution s;
l = s.addTwoNumbers(l1, l2);
while (l != NULL){
cout << l->val << endl;
l = l->next;
}
while ();
}

运行结果:

最后实现不等长有进位的求和,即实现题目要求(注意最后一位有进位的情况)

 #include <iostream>

 using namespace std;

 struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { int val1 = , val2 = , carry = ; ListNode *l = NULL;
ListNode *l_end = NULL;
ListNode *l_new = NULL; val1 = l1->val;
val2 = l2->val;
l1 = l1->next;
l2 = l2->next;
l_new = (ListNode *)new ListNode((val1 + val2 + carry) % );
l_new->next = NULL;
l = l_new;
carry = (val1 + val2 + carry) / ;
l_end = l;
while (l1 != NULL || l2 != NULL){
if (l1 == NULL){
val2 = l2->val;
l2 = l2->next;
val1 = ;
}
else if (l2 == NULL){
val1 = l1->val;
l1 = l1->next;
val2 = ;
}
else{
val1 = l1->val;
val2 = l2->val;
l1 = l1->next;
l2 = l2->next;
} l_new = (ListNode *)new ListNode((val1 + val2 + carry)%);
l_new->next = NULL;
carry = (val1 + val2 + carry) / ; l_end->next = l_new;
l_end = l_new;
}
if (carry != ){
l_new = (ListNode *)new ListNode(carry);
l_new->next = NULL;
l_end->next = l_new;
l_end = l_new;
}
return l; }
}; int main()
{
ListNode *l1, *l2;
ListNode *l;
l1 = (ListNode *)new ListNode();
l2 = (ListNode *)new ListNode();
l1->next = (ListNode *)new ListNode();
l2->next = (ListNode *)new ListNode();
l1->next->next = (ListNode *)new ListNode();
l2->next->next = (ListNode *)new ListNode();
Solution s;
l = s.addTwoNumbers(l1, l2);
while (l != NULL){
cout << l->val << endl;
l = l->next;
}
while ();
}

运行结果:

因为是一边学C++,一边刷leetcode,所以有什么问题,十分感谢您能指点。

leetcode刷题: 002 Add Two Numbers的更多相关文章

  1. LeetCode刷题系列——Add Two Numbers

    题目链接 这个题目很简单,归并而已,好久没练编程,居然忘了在使用自定义类型前,要进行初始化(new操作). class ListNode{ int val; ListNode next; ListNo ...

  2. LeetCode第二题:Add Two Numbers

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

  3. 刷题2. Add Two Numbers

    一.题目要求 You are given two non-empty linked lists representing two non-negative integers. The digits a ...

  4. LeetCode刷题笔录Add Binary

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  5. C#LeetCode刷题-数学

    数学篇 # 题名 刷题 通过率 难度 2 两数相加   29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 ...

  6. LeetCode刷题指南(字符串)

    作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...

  7. LeetCode刷题总结-数组篇(上)

    数组是算法中最常用的一种数据结构,也是面试中最常考的考点.在LeetCode题库中,标记为数组类型的习题到目前为止,已累计到了202题.然而,这202道习题并不是每道题只标记为数组一个考点,大部分习题 ...

  8. LeetCode刷题总结-数组篇(中)

    本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...

  9. LeetCode刷题总结-数组篇(下)

    本期讲O(n)类型问题,共14题.3道简单题,9道中等题,2道困难题.数组篇共归纳总结了50题,本篇是数组篇的最后一篇.其他三个篇章可参考: LeetCode刷题总结-数组篇(上),子数组问题(共17 ...

  10. LeetCode刷题总结-树篇(上)

          引子:刷题的过程可能是枯燥的,但程序员们的日常确不乏趣味.分享一则LeetCode上名为<打家劫舍 |||>题目的评论: 如有兴趣可以从此题为起点,去LeetCode开启刷题之 ...

随机推荐

  1. HDU 3006

    http://acm.hdu.edu.cn/showproblem.php?pid=3006 注意到集合内数字最大只有14,状态压缩一下,然后枚举出所有状态 #include <iostream ...

  2. 在Bootstrap中 强调相关的类

    .text-muted:提示,使用浅灰色(#999) .text-primary:主要,使用蓝色(#428bca) .text-success:成功,使用浅绿色(#3c763d) .text-info ...

  3. word转pdf字体格式变乱的问题

    完成word转pdf的功能之后,本地测试没问题,然后发布到服务器上,就遇到了字体变乱的问题,如下: 由于我本地发布后导出没有出现同样情况,而服务器和本地的最大区别在于字体库,于是,把服务器上关于需要用 ...

  4. bash的操作环境[转]

          Bash Shell 的操作环境: 是否记得我们登陆主机的时候,屏幕上头会有一些说明文字,告知我们的 Linux 版本啊什么的, 还有,登陆的时候我们还可以给予用户一些信息或者欢迎文字呢. ...

  5. 【Unity3D基础教程】给初学者看的Unity教程(一):GameObject,Compoent,Time,Input,Physics

    作者:王选易,出处:http://www.cnblogs.com/neverdie/  欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点推荐.谢谢! Unity3D重要模块的类图 最近刚刚完成了一 ...

  6. IO-同步,异步,阻塞,非阻塞

    IO-同步,异步,阻塞,非阻塞1.什么是IO数据在系统内核(kernel)和用户进程之间的传递,称为IO. 2.IO操作步骤以read为例,涉及两个系统对象,调用IO的process(or threa ...

  7. unity, terrain道出为obj

    http://wiki.unity3d.com/index.php?title=TerrainObjExporter

  8. HackerRank "Square Subsequences" !!!

    Firt thought: an variation to LCS problem - but this one has many tricky detail. I learnt the soluti ...

  9. bzoj3822: 文学

    Description 巨酱和主席是一对好朋友.他们都很喜欢读书,经常一起阅读相关领域书籍,进行系统的学习.一天主席列出了一份列表,里面共 p 本书,其中不乏<约翰克里斯多夫>,<名 ...

  10. 使用nodejs防止csurf攻击的方法

    一.CSRF是什么? CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click attack/session riding,缩写为:CSR ...