[LeetCode] 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
这题是链表遍历问题,容易处理。
#include <iostream>
using namespace std;
/**
* Definition for singly-linked list.
*/
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
if(l1==NULL) return l2;
if(l2==NULL) return l1;
int addOne=;
ListNode ret();
ListNode *p1=l1,*p2=l2,*pret=&ret;
while(){
if(p1==NULL&&p2==NULL&&addOne==) break;
if(p1==NULL&&p2==NULL){
pret->next = new ListNode((addOne)%);
pret = pret->next;
addOne = ;
continue;
}
if(p1==NULL){
pret->next = new ListNode((p2->val + addOne)%);
pret = pret->next;
addOne = (p2->val + addOne)/;
p2=p2->next;
continue;
}
if(p2==NULL){
pret->next = new ListNode((p1->val + addOne)%);
pret = pret->next;
addOne = (p1->val + addOne)/;
p1=p1->next;
continue;
}
pret->next = new ListNode((p1->val + p2->val + addOne)%);
pret = pret->next;
addOne = (p1->val + p2->val + addOne)/;
p1=p1->next;
p2=p2->next;
}
return ret.next;
}
}; int main()
{ return ;
}
[LeetCode] Add Two Numbers 链表的更多相关文章
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- Leetcode:Add Two Numbers分析和实现
Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点.比如345表示为链表5->4->3.而我们需要做的就是将两条链表代表的 ...
- [LeetCode] Add Two Numbers题解
Add Two Numbers: You are given two non-empty linked lists representing two non-negative integers. Th ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- LeetCode: Add Two Numbers 解题报告
Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...
- 【LeetCode】2.Add Two Numbers 链表数相加
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- leetcode 2 Add Two Numbers(链表)
数字反过来这个没有什么麻烦,就是镜像的去算十进制加法就可以了,然后就是简单的链表. /** * Definition for singly-linked list. * struct ListNode ...
- [LeetCode]2. Add Two Numbers链表相加
注意进位的处理和节点为null的处理 public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int flag = 0; ListNode ...
随机推荐
- 为什么选择Redis
1)Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,zset,hash等数据结构的存储. 2)Redis支持master-slave(主-从)模式应用 3)Redi ...
- 1、python的基础
一.python组成 python程序的内容主要由变量.数据.关键字.操作符组成. 二.变量 在python中,变量指的是其指向的数据是可变的. 首先我们要了解一下python的内存管理.数据创建后就 ...
- java NIO简介
1)java nio简介 nio 是 java New IO 的简称,在 jdk1.4 里提供的新 api . Sun 官方标榜的特性如有:为所有的原始类型提供 (Buffer) 缓存支持:字符集编码 ...
- Windows下如何用CMD命令跳转到指定的目录下
以Window7为例说明,想要跳转到I:\adt-bundle-windows-x86-20130219\sdk\platform-tools目录下. 1.在运行中打开CMD命令窗口如下图所示: 2. ...
- springboot学习资料汇总
收集Spring Boot相关的学习资料,Spring Cloud点这里 推荐博客 纯洁的微笑 程序猿DD liaokailin的专栏 Spring Boot 揭秘与实战 系列 catoop的专栏 简 ...
- logback mybatis 打印sql语句
logbac.xml 文件的基础配置参考的园友的 http://www.cnblogs.com/yuanermen/archive/2012/02/13/2349609.html 然后hibernat ...
- export、export default、module.export区别
在es6里面定义模块,导出模块时可以使用export.export default 这2者区别: 在同一个文件里面可以有多个export, 一个文件里面只能有1个export default //a. ...
- 创建 PSO
TechNet 库 Windows Server Windows Server 2008 R2 und Windows Server 2008 浏览 Windows Server 技术 Active ...
- Quorum机制与NRW算法总结
Quorum机制与NRW算法总结 1.Quorum机制 Quorum,原指为了处理事务.拥有做出决定的权力而必须出席的众议员或参议员的数量(一般指半数以上). 2.NRW算法 NRW算法是基于Quor ...
- ios开发学习笔记003-流程控制和类型转换
流程控制 顺序结构.选择结构.循环结构 1.顺序结构 程序默认是顺序执行的. 2.选择结构 if选择语句 第一种情况 if(条件)//条件成立执行下面语句 { //语句 } 第二种情况 if(条件)/ ...