You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)

Output: 7 -> 0 -> 8

思路:从左边对齐,开始相加,将产生的进位置为下一个指针的初始值,这里主要需要考虑到几种情况,两个链表相等或不等两类,

最重要的是根据进位决定next指针是否为空或者是一个新节点。

/**
* 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) {
ListNode * head = new ListNode(0);
ListNode * beg = head;
int pre=0;
while(l1!=NULL||l2!=NULL){
if(l1!=NULL&&l2!=NULL){
head->val = head->val+l1->val+l2->val;
pre = head->val/10;
head->val = (head->val)%10;
l1 = l1->next;
l2 = l2->next;
if(l1==NULL&&l2==NULL&&pre==0)head->next = NULL;//这个if很重要
else head->next = new ListNode(pre);
head = head->next;
}
else if(l1!=NULL&&l2==NULL){
head->val = head->val+l1->val;
pre = head->val/10;
head->val = (head->val)%10;
l1 = l1->next;
if(l1==NULL&&pre==0)head->next = NULL;
else head->next = new ListNode(pre);
head = head->next;
}
else if(l1==NULL&&l2!=NULL)
{
head->val = head->val+l2->val;
pre = head->val/10;
head->val = (head->val)%10;
l2 = l2->next;
if(l2==NULL&&pre==0)head->next = NULL;
else head->next = new ListNode(pre);
head = head->next;
}
else{
head->val = pre;
return beg;
}
}
return beg;
}
};

21-Add Two Numbers-Leetcode的更多相关文章

  1. Add Two Numbers LeetCode Java

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

  2. Add two numbers [LeetCode]

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

  3. Add Two Numbers ---- LeetCode 002

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

  4. LeetCode 面试:Add Two Numbers

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

  5. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  6. [LeetCode] Add Two Numbers 两个数字相加

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

  7. LeetCode Add Two Numbers II

    原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...

  8. leetcode 第二题Add Two Numbers java

    链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...

  9. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  10. LeetCode:1. Add Two Numbers

    题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...

随机推荐

  1. 认识spring security

    在一个系统中认证和授权是常有的事情,现在比较流行的框架有spring security.shiro等等.他们都能很好的帮助我们完成认证和授权的功能.那么假如说让我们自己完成一个登录那么应该大致的流程是 ...

  2. 鸿蒙轻内核M核的故障管家:Fault异常处理

    摘要:本文先简单介绍下Fault异常类型,向量表及其代码,异常处理C语言程序,然后详细分析下异常处理汇编函数实现代码. 本文分享自华为云社区<鸿蒙轻内核M核源码分析系列十八 Fault异常处理& ...

  3. MySQL 的架构与组件

    MySQL 的逻辑架构图设计图 连接/线程处理:管理客户端连接/会话[mysql threads] 解析器:通过检查SQL查询中的每个字符来检查SQL语法,并为每个SQL查询生成  SQL_ID. 此 ...

  4. (总结)Linux下su与su -命令的本质(转)

    转载地址:http://www.ha97.com/4001.html 本人以前一直习惯直接使用root,很少使用su,前几天才发现su与su -命令是有着本质区别的! 大部分Linux发行版的默认账户 ...

  5. no space left on device 磁盘空间不足

    新挂载的目录,创建文件提示:no space left on device 1.执行命令:df -h ,查看盘是否挂载成功 2.用history命令查看历史命令,尴尬的发现挂载前忘记格式化了 3.取消 ...

  6. SpringCloud微服务实战——搭建企业级开发框架(十四):集成Sentinel高可用流量管理框架【限流】

      Sentinel 是面向分布式服务架构的高可用流量防护组件,主要以流量为切入点,从限流.流量整形.熔断降级.系统负载保护.热点防护等多个维度来帮助开发者保障微服务的稳定性. Sentinel 具有 ...

  7. 3D 穿梭效果?使用 CSS 轻松搞定

    背景 周末在家习惯性登陆 Apex,准备玩几盘.在登陆加速器的过程中,发现加速器到期了. 我一直用的腾讯网游加速器,然而点击充值按钮,提示最近客户端升级改造,暂不支持充值(这个操作把我震惊了~).只能 ...

  8. MongoDB与MySQL效率对比

    本文主要通过批量与非批量对比操作的方式介绍MongoDB的bulkWrite()方法的使用.顺带与关系型数据库MySQL进行对比,比较这两种不同类型数据库的效率.如果只是想学习bulkWrite()的 ...

  9. hdfs command

    hadoop fs -ls hdfs dfs -mkdir -p /user/$(whoami) hdfs dfs -chown -R $(whoami) /user/$(whoami) hdfs d ...

  10. MySQL基础语句(修改)

    ①INSERT INSERT INTO students (class_id, name, gender, score) VALUES (2, '大牛', 'M', 80); 向students表插入 ...