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. Sharding-JDBC基本使用,整合Springboot实现分库分表,读写分离

    结合上一篇docker部署的mysql主从, 本篇主要讲解SpringBoot项目结合Sharding-JDBC如何实现分库分表.读写分离. 一.Sharding-JDBC介绍 1.这里引用官网上的介 ...

  2. 算法:杨辉三角(Pascal's Triangle)

    一.杨辉三角介绍 杨辉三角形,又称帕斯卡三角形.贾宪三角形.海亚姆三角形.巴斯卡三角形,是二项式系数的一种写法,形似三角形,在中国首现于南宋杨辉的<详解九章算法>得名,书中杨辉说明是引自贾 ...

  3. Java并发:ReadWriteLock 读写锁

    读写锁在同一时刻可以允许多个线程访问,但是在写线程访问,所有的读线程和其他写线程均被阻塞. 读写锁不像 ReentrantLock 那些排它锁只允许在同一时刻只允许一个线程进行访问,读写锁可以允许多个 ...

  4. P4430 小猴打架

    P4430 小猴打架 题目意思就是让你求,在网格图中(任意两点都有边)的生成树的个数(边的顺序不同也算不同的方案). 首先我们考虑一个生成树,由于一定有n-1条边,单单考虑添加边的顺序,根据乘法原理, ...

  5. HTML bootstrap 模态对话框添加用户

    HTML 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> ...

  6. 端口扫描工具 nmap 使用手册

    0x00 主机发现 -sL 仅仅是显示,扫描的IP数目,不会进行任何扫描 -sn ping扫描,即主机发现 -Pn 不检测主机存活 -PS/PA/PU/PY[portlist] TCP SYN Pin ...

  7. Git基本教程

    git的发展 Git 两周开发 Linus开发,主要是为了管理大量人员维护代码 Git分布式版本控制系统 基本命令 history:查看之前用过的命令 vimtutor git配置 查看配置 git ...

  8. VIM处理工具与正则表达式

    *本文中/data目录为训练目录 1.在vim中设置TAB缩进为四个字符 打开vim 输入:set tabstop=4 2.复制/etc/rc.d/init.d/functions文件至/tmp/,替 ...

  9. feign微服务调用携带浏览器信息(header、cookie)

    import feign.RequestInterceptor; import feign.RequestTemplate; import org.apache.commons.collections ...

  10. Part 15 AngularJS ng init directive

    The ng-init directive allows you to evaluate an expression in the current scope.  In the following e ...