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. Python环境配置详细步骤以及第一个程序

    打开python官网:https://www.python.org/ 在官网找与自己电脑系统匹配的版本路径  这里以python3.7.2版本为例: 下载完成后,使用管理员身份进行安装:  打开命令提 ...

  2. 数列极限计算中运用皮亚诺Taylor展开巧解

    这是讲义里比较精华的几个题目,今晚翻看也是想到了,总结出来(处理k/n2形式). 推广式子如下: 例题如下:

  3. max-points-on-a-line leetcode C++

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  4. java实现微信分享

    之前项目中涉及到了微信分享的功能,然后总结下供有需要的朋友参考下. 在做之前可以先看下<微信JS-SDK说明文档>,大致了解下.我自己的工程目录是 1.HttpService和HttpSe ...

  5. SpringMVC配置知识点

    SpringMVC原生知识点 通过idea新建一个SpringMVC的Project(新建普通的项目就行了) 填写完之后Finish就行了 (实际开发不会这么用,这么做是为了理解!) 然后就是Spri ...

  6. C++ pragma once 与 ifndef 用法区别

    #pragma once 与 #ifndef 的作用 (1)在C/C++中,在使用预编译指令#include的时候,为了防止重复引用造成二义性. (2)在能够支持这两种方式的编译器上,二者并没有太大的 ...

  7. 升级JDK8的坎坷之路

    为更好的适应JAVA技术的发展,使用更先进及前沿的技术.所以推出将我们现在使用的JDK1.6(1.7)及tomcat6(7)升级至JDK1.8及tomcat8,使我们的系统获得更好的性能,更好适应未来 ...

  8. SQL 添加列,删除列,修改列的类型

    alter table 表名 add 列名 数据类型 如:alter table student add nickname char(20) alter table tableName(表名) add ...

  9. SQL Server2019数据库备份与还原脚本,数据库可批量备份

    前言 最近公司服务器到期,需要进行数据迁移,而数据库属于多而繁琐,通过图形化界面一个一个备份所需时间成本很大,所以想着写一个sql脚本来执行. 开始 数据库单个备份 数据库批量备份 数据库还原 数据库 ...

  10. 调用企业微信API拨打紧急通知电话

    # 前提条件:企业信息:行业类型必须属于"医疗"大类,客服反馈说目前不支持其他行业# 准备工作:https://work.weixin.qq.com/api/doc/90000/9 ...