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

class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
int carry = ;
ListNode *l3 = new ListNode();
ListNode *p3 = l3;
while(l1 !=NULL || l2!= NULL){
if(l1 != NULL){
carry += l1->val;
l1 = l1->next;
}
if(l2 != NULL){
carry += l2->val;
l2 = l2->next;
} l3->next = new ListNode(carry % );
carry = carry/;
l3 = l3->next;
}
if(carry == )
l3->next = new ListNode();
return p3->next;
}
};

【leetcode】 Add Two Numbers的更多相关文章

  1. 【leetcode】Add Two Numbers

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

  2. 【题解】【链表】【Leetcode】Add Two Numbers

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

  3. 【leetcode】Add Two Numbers(middle) ☆

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

  4. 【LeetCode】Add Two Numbers(两数相加)

    这道题是LeetCode里的第2道题. 题目要求: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将 ...

  5. 【LeetCode445】 Add Two Numbers II★★

    题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...

  6. 【LeetCode】386. Lexicographical Numbers 解题报告(Python)

    [LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  7. 【Leetcode】【Medium】Add Two Numbers

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

  8. 【链表】Add Two Numbers

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

  9. 【leetcode】1215.Stepping Numbers

    题目如下: A Stepping Number is an integer such that all of its adjacent digits have an absolute differen ...

随机推荐

  1. Knowledge-Defined Networking

    知识定义的网络(Knowledge-Defined Networking) 来源:ACM SIGCOMM Computer Communication Review 年份:2017 是什么:容纳和利用 ...

  2. [BUAA_SE_2017]第零次博客

    结缘计算机 你为什么选择计算机专业?你认为条件如何? 计算机是你喜欢的领域吗?是你擅长的领域吗? 说来也巧,高考选择专业时并不知道自己会这般喜欢计算机这个专业,却将其填在了北航的第一志愿. 第一次接触 ...

  3. Selenium的自我总结2_元素基本操作

    对于Selenium的基本元素的操作,就自己的了解做了一个基本的介绍,这篇直接上代码,针对一个页面如何操作写了些基本的操作脚本,希望对初学者有一定的帮助,也希望通过这些总结让自己有一些清晰的认识和了解 ...

  4. node的cookie-parser和express-session

    let express = require('express'); let cookieParser = require('cookie-parser'); let expressSession = ...

  5. “一战通offer”互联网实习季编程挑战

    1.字符串变形 对于一个给定的字符串,我们需要在线性(也就是O(n))的时间里对它做一些变形.首先这个字符串中包含着一些空格,就像"Hello World"一样,然后我们要做的是把 ...

  6. Delphi的关键字

    Constructor;构造器,定义构造函数使用Constructor关键字

  7. Lodop导出excel及提示成功【回调和直接返回值】

    高版本的火狐和谷歌不再支持np插件之后,Lodop公司推出了C-Lodop,解决了这些浏览器不能用Lodop插件方式打印的问题,相比较Lodop插件,C-Lodop由于是以服务的形式出现,返回值不能直 ...

  8. Nginx referer防盗链模块

    L75 referer模块 ngx_http_referer_module 默认编译进nginx valid_referers 指令 Syntax: valid_referers none | blo ...

  9. 数据库左右连接on后的限制条件问题

    测试环境: MySQL 5.7.19 HeidiSQL 9.3 数据库界面连接工具(挺好用的) 碰到的问题是: Select * from t1 left outer join t2 on t1.id ...

  10. bzoj 3224: Tyvj 1728 普通平衡树 && loj 104 普通平衡树 (splay树)

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 思路: splay树模板题: 推荐博客:https://blog.csdn.ne ...