2_Add Two Numbers --LeetCode
原题如下:

思路:在一个while中遍历两个链表,直到最长的链表为空,或者没有进位。每一步获取两个链表对应的结点的值a,b,然后相加a+b。如果上一步又进位,那就加a+b+1,若由于进位加1后还产生进位,则设置进位标识位为true。如果a+b大于9,也要设置进位标识为true。
代码如下:
/**
* 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(NULL==l1 && NULL==l2)
{
ListNode* LN = (ListNode*)malloc(sizeof(ListNode));
LN->val = ;
LN->next = NULL;
return LN;
} if(NULL == l2 && NULL !=l1)
return l1; if(NULL == l1 && NULL !=l2)
return l2; bool carry = false; //进位标识符
ListNode* p = l1,*q = l2,* L = NULL,*s = NULL,*pl; //创建头结点,后面会删除掉。
if(!(L = (ListNode*)malloc(sizeof(ListNode))))
return L;
L->val = ;
L->next = NULL;
pl = L;
while(p!=NULL || q!=NULL || carry)
{ int pos1=,pos2=,remain=, sum = ; //以下两个if是获取两个链表中的值
if(p!=NULL)
{
pos1 = p->val;
p=p->next;
} if(q!=NULL)
{
pos2 = q->val;
q=q->next;
} //相加
sum = pos1+pos2;
//求余
remain = sum%;
//创建结点
if(!(s = (ListNode*)malloc(sizeof(ListNode))))
return s;
//如果上一步又进位
if(carry){
//再次判断进位后是否还进位
if(remain+>){
s->val=;
carry = true;
}else{
s->val = remain+;
carry = false;
} }else
s->val=remain;
//添加结点到链表中
s->next = NULL;
pl->next = s;
pl = s; //判断是否进位
if(sum>)
carry = true;
} //删除结点
pl = L;
L = L->next;
free(pl); return L;
}
};
2_Add Two Numbers --LeetCode的更多相关文章
- 2-Add Two Numbers @LeetCode
2-Add Two Numbers @LeetCode 题目 思路 题目中得到的信息有: 这是两个非负数,每位分别保存在链表的一个结点上: 逆序保存,从低位到高位依次. 一般整数的相加都是从低往高进行 ...
- LeetCode: 2_Add Two Numbers | 两个链表中的元素相加 | Medium
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- Add Two Numbers LeetCode Java
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- Add two numbers [LeetCode]
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- Sum Root to Leaf Numbers [LeetCode]
Problem description: http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ Basic idea: To store ...
- Add Two Numbers ---- LeetCode 002
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- Sum Root to Leaf Numbers——LeetCode
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- Compare Version Numbers leetcode
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- Sum Root to Leaf Numbers leetcode java
题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...
随机推荐
- SQLServer2008修改sa密码的方法与SQL server 2008数据库的备份与还原
sa密码的修改转载自:http://blog.csdn.net/templar1000/article/details/20211191 SQL server 2008数据库的备份与还原转自 :htt ...
- 介绍一种非常好用汇总数据的方式GROUPING SETS
介绍 对于任何人而言,用T-SQL语句来写聚会查询都是工作中重要的一环.我们大家也都很熟悉GROUP BY子句来实现聚合表达式,但是如果打算在一个结果集中包含多种不同的汇总结果,可能会比较麻烦.我将举 ...
- 洛谷 [P1387] 最大正方形
本题非常有趣. (n^6) 枚举四个端点,每次遍历矩阵求解. (n^4) 先处理前缀和,枚举四个端点,每次比较前缀和和正方形面积. (n^3) 枚举左上方端点,在枚举边长,前缀和优化 (n^2logn ...
- BZOJ 2595: [Wc2008]游览计划 [DP 状压 斯坦纳树 spfa]【学习笔记】
传送门 题意:略 论文 <SPFA算法的优化及应用> http://www.cnblogs.com/lazycal/p/bzoj-2595.html 本题的核心就是求斯坦纳树: Stein ...
- I/O多路转接模型
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- chrome无法登陆账号,显示操作超时的解决方案
起因 今天重装了下windows操作系统,准备登陆chrome浏览器,以同步各种插件(你懂的),结果是...无法登陆账号,显示操作超时,真是无语了. 碰到了这个问题第一个直觉是:FQ.突然想到如果修改 ...
- 树莓派上运行.net core 2.0程序
记录中 参考: https://www.cnblogs.com/songxingzhu/p/7399991.html https://www.cnblogs.com/goodfulcom/p/7624 ...
- vscode php跳转
最近在写一个php项目,最后选定使用vscode编辑器,然后研究了一下断点调试.格式代码.点击跳转 以下是配置步骤,记录一下 1.代码格式化及跳转 1.前提条件:安装7.0以上版本php, ...
- caffe中Makefile.config详解
## Refer to http://caffe.berkeleyvision.org/installation.html # Contributions simplifying and improv ...
- Golang学习 - strconv 包--数据类型转换
// 将布尔值转换为字符串 true 或 false func FormatBool(b bool) string // 将字符串转换为布尔值 // 它接受真值:1, t, T, TRUE, true ...