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 ...
随机推荐
- nodejs爬虫笔记(一)---request与cheerio等模块的应用
目标:爬取慕课网里面一个教程的视频信息,并将其存入mysql数据库.以http://www.imooc.com/learn/857为例. 一.工具 1.安装nodejs:(操作系统环境:WiN 7 6 ...
- django xadmin 集成DjangoUeditor富文本编辑器
本文档记录自己的学习历程! 介绍 Ueditor HTML编辑器是百度开源的在线HTML编辑器,功能非常强大 额外功能 解决图片视频等无法上传显示问题 Ueditor下载地址 https://gith ...
- 使用autoc js生成文章目录(侧边)导航栏
介绍: autocjs 是一个专门用来生成文章目录(Table of Contents)导航的工具.autocjs 会查找文章指定区域中的所有 h1~h6 的标签,并自动分析文章的层次结构,生成文章的 ...
- dnion的remap.conf文件
# # URL Remapping Config File # # Using remap.config allows you to accomplish two things: # # 1) Rew ...
- 编译和解释性语言和python运行方式
1.编译型语言和解释性语言 编译型语言:在执行之前需要一个专门的编译过程,把程序编译成为机器语言的文件,运行时不需要重新翻译,直接使用编译的结果就行了.程序执行效率高,依赖编译器,跨平台性差些.如C. ...
- 浅尝一个排程引擎Optaplanner,前序。
当码农有10多年了,由建筑行业软件,各种MIS,通用物流定制平台,CCTV客户端(是闭路电视,不是央视喔)啥都做过.最后小试一下创业,不过那都是闹着玩的,不到一年就回到码农的队列,重拾搬砖的行当.近些 ...
- web框架之Django基础
1. Django的简介 Django是一个由python写成的开放源代码的Web应用框架. Django的目的是使常见的Web开发任务,快速和容易. 2. Django框架的特点 1. 遵循MVC开 ...
- Go笔记-结构、类型、常量
[类型] 1.可以包含数据的变量(或常量),可以使用不同的数据类型或类型来保存数据.使用 var 声明的变量的值会自动初始化为该类型的零值.类型定义了某个变量的值的集合与可对其进行操作的集合. 2 ...
- bzoj 1975: [Sdoi2010]魔法猪学院 [k短路]
1975: [Sdoi2010]魔法猪学院 裸题... 被double坑死了 #include <iostream> #include <cstdio> #include &l ...
- POJ 2079 Triangle [旋转卡壳]
Triangle Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 9525 Accepted: 2845 Descript ...