原题如下:

思路:在一个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的更多相关文章

  1. 2-Add Two Numbers @LeetCode

    2-Add Two Numbers @LeetCode 题目 思路 题目中得到的信息有: 这是两个非负数,每位分别保存在链表的一个结点上: 逆序保存,从低位到高位依次. 一般整数的相加都是从低往高进行 ...

  2. LeetCode: 2_Add Two Numbers | 两个链表中的元素相加 | Medium

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  3. Add Two Numbers LeetCode Java

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  4. Add two numbers [LeetCode]

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  5. Sum Root to Leaf Numbers [LeetCode]

    Problem description: http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ Basic idea: To store ...

  6. Add Two Numbers ---- LeetCode 002

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  7. 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 ...

  8. Compare Version Numbers leetcode

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  9. 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 ...

随机推荐

  1. apache编译安装参数说明

    apache编译安装参数说明 ./configure //配置源代码树--prefix=/usr/local/apache2 //体系无关文件的顶级安装目录prefix ,也就apache的安装目录. ...

  2. 模块(module)

    1.模块加载 import math  //import后面跟模块名 from module1 import module11  //module1是一个大模块,里边有子模块module11,调用这个 ...

  3. 使用H2数据库进行单元测试

    背景 H2 数据库是一个开源的嵌入型内存数据库,采用纯Java语言实现: 程序非常小巧轻便,整个完整的Jar包也只有1.5M左右,很容易集成到项目中. 官网地址 http://www.h2databa ...

  4. Html5五子棋

    1.效果图 2.代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...

  5. mkdir -p 参数的使用

    ssh root@%s -o ConnectTimeout=2 "ssh root@%s ConnectTimeout=2 "if [ ! -d /root/scripts ]; ...

  6. 新建play项目eclipsify后导入eclipse后无法debug调试

    Error occurred during initialization of VMagent library failed to init: jdwpERROR: Cannot load this ...

  7. AdobeFlashBuilder还不如AdobeFlashProfessional写actionscript体验好

    AdobeFlashBuilder还不如AdobeFlashProfessional写actionscript体验好. 这真是奇怪了.

  8. Spring源码情操陶冶-PathMatchingResourcePatternResolver路径资源匹配溶解器

    本文简单的分析下spring对某个目录下的class资源是如何做到全部的加载 PathMatchingResourcePatternResolver#getResources PathMatching ...

  9. HDU 2865 Birthday Toy [Polya 矩阵乘法]

    传送门 题意: 相邻珠子不能相同,旋转等价.$n$个珠子$k$中颜色,求方案数 首先中间珠子$k$种选择,$k--$如果没有相邻不同的限制,就和$POJ\ 2154$一样了$|C(f)|=k^{\#( ...

  10. maven的下载安装,配置本地仓库

    maven的下载安装 下载地址:http://maven.apache.org/download.cgi 下载完成后解压到某一个目录 配置环境变量 第一个环境变量 MAVEN_HOME A:\mave ...