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

这题虽然归类到Medium 但是难度不大,只是要考虑的东西有点多,比如:两个list不一定一样长,进位这两个问题。

因为两个数字是逆序存到链表里的,所以这大大简化了问题难度,因为只需要向着链表遍历方向往前进位就可以了。

void addIter(ListNode *l1, ListNode *l2, ListNode *prev, int carry) {
if (!l1 && !l2 && carry == ) return;
ListNode *node;
int val = ;
if (l1) {
val += l1->val;
}
if (l2) {
val += l2->val;
}
val += carry;
carry = val / ;
val = val % ; node = new ListNode(val);
if (prev) {
prev->next = node;
} addIter(l1 ? l1->next : NULL, l2 ? l2->next : NULL, node, carry);
} ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode *ret = new ListNode();
addIter(l1, l2, ret, );
return ret->next;
}

[LeetCode] Add Two Numbers的更多相关文章

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

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

  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 II

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

  4. LeetCode: Add Two Numbers 解题报告

    Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...

  5. Leetcode:Add Two Numbers分析和实现

    Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点.比如345表示为链表5->4->3.而我们需要做的就是将两条链表代表的 ...

  6. [LeetCode] Add Two Numbers题解

    Add Two Numbers: You are given two non-empty linked lists representing two non-negative integers. Th ...

  7. [Leetcode] Add two numbers 两数之和

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

  8. LeetCode——Add Two Numbers

    Question:You are given two linked lists representing two non-negative numbers. The digits are stored ...

  9. [LeetCode] Add Two Numbers 链表

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

随机推荐

  1. windows下的getopt/getoptlong函数

    windows下的getopt/getoptlong函数 getopt/getopt_long函数是GNU C中的函数,在linux编程中很常用到.这里就不介绍了. windows下没有找到类似的函数 ...

  2. 转:bwa的使用方法

    bwa的使用需要两中输入文件:    Reference genome data(fasta格式 .fa, .fasta, .fna)    Short reads data (fastaq格式 .f ...

  3. No enum constant org.apache.ibatis.type.JdbcType.xxx 错误

    配置文件Mapper中JdbcType错误,有可能是大小写,有可能是拼写错误

  4. 【smarty项目源码】模拟smarty模版文件的解析过程

    <?php class MyMiniSmarty{ //模版文件的存放路径 var $template_dir="./templates/"; //编译文件的存放路径 ,编译 ...

  5. TCP三次握手原理详解

    TCP/IP协议不是TCP和IP这两个协议的合称,而是指因特网整个TCP/IP协议族. 从协议分层模型方面来讲,TCP/IP由四个层次组成:网络接口层.网络层.传输层.应用层. TCP协议:即传输控制 ...

  6. 第十天 多进程、协程(multiprocessing、greenlet、gevent、gevent.monkey、select、selector)

    1.多进程实现方式(类似于多线程) import multiprocessing import time,threading def thread_run():#定义一个线程函数 print(&quo ...

  7. map的使用

    @Override public List<Map<String, Object>> findSchedule(Date beginTime, Date endTime, Lo ...

  8. eclipse添加字体

    1.打开window—>Preferences—>General—>Appeatance—>Colors and Fonts—>Text Font—>Edit 2. ...

  9. AFNetWorking支持解析html的方法

    在AFURLResponseSeriallzation.m的226行 手动添加@"text/html" 添加之前解锁文件

  10. a byte of python(摘03)

    a byte of python 第七章 模块 想要在其他程序中重用很多函数,那么你该如何编写程序呢? 答案是使用模块. 模块基本上就是一个包含了所有你定义的函数和变量的文件.为了在其他程序中重用模块 ...