leetcode刷题: 002 Add Two Numbers
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的更多相关文章
- LeetCode刷题系列——Add Two Numbers
题目链接 这个题目很简单,归并而已,好久没练编程,居然忘了在使用自定义类型前,要进行初始化(new操作). class ListNode{ int val; ListNode next; ListNo ...
- LeetCode第二题:Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- 刷题2. Add Two Numbers
一.题目要求 You are given two non-empty linked lists representing two non-negative integers. The digits a ...
- LeetCode刷题笔录Add Binary
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- C#LeetCode刷题-数学
数学篇 # 题名 刷题 通过率 难度 2 两数相加 29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 ...
- LeetCode刷题指南(字符串)
作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...
- LeetCode刷题总结-数组篇(上)
数组是算法中最常用的一种数据结构,也是面试中最常考的考点.在LeetCode题库中,标记为数组类型的习题到目前为止,已累计到了202题.然而,这202道习题并不是每道题只标记为数组一个考点,大部分习题 ...
- LeetCode刷题总结-数组篇(中)
本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...
- LeetCode刷题总结-数组篇(下)
本期讲O(n)类型问题,共14题.3道简单题,9道中等题,2道困难题.数组篇共归纳总结了50题,本篇是数组篇的最后一篇.其他三个篇章可参考: LeetCode刷题总结-数组篇(上),子数组问题(共17 ...
- LeetCode刷题总结-树篇(上)
引子:刷题的过程可能是枯燥的,但程序员们的日常确不乏趣味.分享一则LeetCode上名为<打家劫舍 |||>题目的评论: 如有兴趣可以从此题为起点,去LeetCode开启刷题之 ...
随机推荐
- (转)解决Android SDK Manager无法更新或下载太慢问题
原帖地址:http://blog.csdn.net/exlsunshine/article/details/22208857 天朝的网络...哎~真是无语...还好最近装了谷歌的chrome浏览器+红 ...
- Turing Tree_线段树&树状数组
Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems abou ...
- 设置ASP.NET页面的运行超时时间详细到单个页面及站点
这篇文章主要介绍了如何设置ASP.NET页面的运行超时时间,包括全局超时时间.单个站点超时时间.单个页面请求超时时间,需要的朋友可以参考下 全局超时时间 服务器上如果有多个网站,希望统一设置一 ...
- Unity3D DllNotFoundException/System.DllNotFoundException
Unity System.DllNotFoundException Unity Fallback handler could not load library D:/91yGame/SparrowCD ...
- Scalding初探之二:动手来做做小实验
输入文件 Scalding既可以处理HDFS上的数据,也可以很方便地在本地运行处理一些test case便于debug,Source有好多种 1 TextLine(filename) TextLine ...
- iOS常用设计模式和机制之代理
Delegate : 1 代理设计模式的使用我们首先需要明白三个要素 *委托方:委托别人去执行某些操作的人(对象) *代理方:被委托区执行某些操作的人(对象) *协议:(protocol)委托方需要代 ...
- XListView刷新
package com.example.da; import java.util.ArrayList;import java.util.List; import com.badu.net.Networ ...
- httpd启动脚本
#!/bin/bash # chkconfig: - . /etc/rc.d/init.d/functions if [ -f /etc/sysconfig/httpd ]; then . /etc/ ...
- mysql出现启动不了问题
查询日志后是‘ Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist’此错误(less ...
- 在Windows上,迁移VisualSVN server
最近在搭建自动化测试框架,顺便了解了一下SVN的搭建.对于一般的使用场景,VisualSVN还是挺方便的,而且上手特别快. 由于是第一个demo,后期要迁移到其他服务器上面,所以就熟悉了一下serve ...