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. NSFetchedResultsControllerDelegate不执行

    熬了2  ,3个小时 才解决这个问题 在进行IM 设置时候 NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@&q ...

  2. Win10/UWP新特性系列—Launcher实现应用间的通信

    UWP中,微软为Windows.System.Launcher启动器新增了很多的功能,以前只能启动App,打开指定扩展名文件,对uri协议的解析,以及当启动的应用没有安装时则会提示前往商店下载等. 如 ...

  3. Collection(数组、字典、集合)

    Collection     -NSArray和NSMutableArray         +array:创建一个空数组         +arrayWithArray:从另一个数组创建新的数组   ...

  4. xml 解析的四种方式

    =========================================xml文件<?xml version="1.0" encoding="GB2312 ...

  5. vim替换指令备忘

    1.  替换当前行中的内容: :s/from/to/    (s即substitude) :s/from/to/     :  将当前行中的第一个from,替换成to.如果当前行含有多个        ...

  6. POJ 1094 (TopoSort)

    http://poj.org/problem?id=1094 题意:该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列.是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序 ...

  7. SQL数据库的十条命令

    --(1)查询每个总学时数 select GradeId,SUM(classHour) from subject group by GradeId order by(SUM(classHour)) - ...

  8. Android 扫一扫----ZXing 的使用

    1. 首先现在ZXing的lib 2. 在Android Studio集成ZXing. public void scan(View view){ startActivityForResult(new ...

  9. ef执行记录原生态代码方法。

    select e; var f = (System.Data.Objects.ObjectQuery<SimpleEntry>)final; var s = f.ToTraceString ...

  10. IOS开发-代码规范

    代码风格的重要性对于一个团队和项目来说不言而喻.网上有许多 Objective-C 的代码风格,但这份简洁而又最符合苹果的规范,同时有助于养成良好的代码习惯,也是我们团队一直遵循的代码风格. 写法没有 ...