原题如下:

思路:在一个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. 【转】shell学习笔记(六)——流程控制之for循环

    基本语法格式: for 变量 in 列表 do 命令行(通常用到循环变量) done ********Linux Shell for循环写法总结******** for((i=1;i<</ ...

  2. postgres允许别人访问连接配置

  3. VBA小技巧

    运用VBA时,可以构造一些函数去实现诸如printf的方便函数. Public Function printf(mask As String, ParamArray tokens()) As Stri ...

  4. 使用xUnit为.net core程序进行单元测试(4)

    第1部分: http://www.cnblogs.com/cgzl/p/8283610.html 第2部分: http://www.cnblogs.com/cgzl/p/8287588.html 第3 ...

  5. Heartbeat实现热备

    1.环境准备:1)主节点:master eth0:192.168.0.201 eth1:192.168.0.03 2)备节点:slave eth0 :192.168.0.215 eth1:192.16 ...

  6. JSONP && CORS

    前天面试被问到了跨域的问题,自我感觉回答的并不理想,下面我就分享一下整理后的总结分享给大家 一.为什么要跨域 安全限制 JavaScript或Cookie只能访问同域下的内容——同源策略 同源策略 下 ...

  7. Google 搜索引擎语法

    Google Hack原理很简单,就是利用搜索引擎强大的搜索能力,来查找一些存在漏洞的网站.要利用Google来查找网站的漏洞自然要学会Google这个搜索引擎的语法了.下面先给大家讲解一下Googl ...

  8. LeetCode - 653. Two Sum IV - Input is a BST

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

  9. How to install tcpping on Linux.md

    To install tcptraceroute on Debian/Ubuntu: $ sudo apt-get install tcptraceroute To install tcptracer ...

  10. Mybatis查询,resultMap="Map" 查询数据有空值,导致整个map为空的问题

    解决方法,不要使用Map接收,使用HashMap或者LinkHashMap,都可以. resultMap="Map" 替换为: resultMap="HashMap&qu ...