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 ...
随机推荐
- 【转】sed 高级用法
首先,应该明白模式空间的定义.模式空间就是读入行所在的缓存,sed对文本行进行的处理都是在这个缓存中进行的.这对接下来的学习是有帮助的. 在正常情况下,sed将待处理的行读入模式空间,脚本中的命令就一 ...
- 【sed & awk 第二版笔记】以州和人名排列_P38
[root@nhserver1 02]# cat listJohn Daggett, 341 King Road, Plymouth MAAlice Ford, 22 East Broadday, R ...
- Erlang调度器细节探析
Erlang调度器细节探析 Erlang的很多基础特性使得它成为一个软实时的平台.其中包括垃圾回收机制,详细内容可以参见我的上一篇文章Erlang Garbage Collection Details ...
- Spring Boot快速入门(最新)
本章通过完成Spring Boot基础项目的构建并实现一个简单的Http请求处理,让大家对Spring Boot有一个初步的了解,并体验其结构简单.开发快速的特性.预计阅读及演练过程将花费约5分钟. ...
- HTML基础知识概括
1.html的概念 HTML是用来描述网页的一种语言. HTML指的是超文本标记语言(HyperText Markup Language) HTML不是一种编程语言,而是一种标记语言(markup l ...
- 使用zii.widgets.CDetailView显示内容
Yii里的CDetailView可以用来显示详细内容,有时会遇到显示的html内容是被转义过的,也就是原本是要显示html样式的,结果显示出来的内容却是把html当作普通文本了. 先看一个CDetai ...
- ABP WebApi 加载错误
[TypeLoadException:类型'Abp.WebApi.Validation.AbpApiValidationFilter'中的方法'ExecuteActionFilterAsync'从程序 ...
- BZOJ 3585: mex [主席树]
3585: mex Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 787 Solved: 422[Submit][Status][Discuss] ...
- Python tutorial阅读之使用 Python 解释器
配置环境变量后,一般可以直接通过Python或指定Python版本号来调用Python. Python 解释器有些操作类似 Unix shell:当使用终端设备(tty)作为标准输入调用时,它交互的解 ...
- zabbix安装步骤
第一步:安装环境 Zabbix要求的环境 组件 版本要求 Apache版本 1 .3.1 2 MySQL版本 5.0.3 PHP版本 5.4.0 本次安装的环境 组件 版本要求 操作系统 CentOS ...