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

Summary: Be careful about the last carry.

     ListNode * handler(int sum, int * carry, ListNode * result, ListNode * * new_head){
if(result == NULL){
result = new ListNode(sum % );
(*new_head) = result;
}else{
result->next = new ListNode(sum % );
result = result->next;
} (*carry) = sum / ;
return result;
}
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode * new_head = NULL;
ListNode * result = NULL;
int carry = ;
while(l1 != NULL || l2 != NULL){
if(l1 == NULL){
int sum = l2->val + carry;
result = handler(sum, &carry, result, &new_head);
l2 = l2->next;
continue;
} if(l2 == NULL){
int sum = l1->val + carry;
result = handler(sum, &carry, result, &new_head);
l1 = l1->next;
continue;
} int sum = l1->val + l2->val + carry;
result = handler(sum, &carry, result, &new_head);
l1 = l1->next;
l2 = l2->next;
} if(carry != )
result->next = new ListNode(carry); return new_head;
}

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 002

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

  3. LeetCode 面试:Add Two Numbers

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

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

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

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

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

  6. LeetCode Add Two Numbers II

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

  7. leetcode 第二题Add Two Numbers java

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

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

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

  9. LeetCode:1. Add Two Numbers

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

随机推荐

  1. Hadoop学习(5)-- Hadoop2

    在Hadoop1(版本<=0.22)中,由于NameNode和JobTracker存在单点中,这制约了hadoop的发展,当集群规模超过2000台时,NameNode和JobTracker已经不 ...

  2. LaTeX学习教程

    本来我对LaTeX不是看好的,毕竟都是命令格式的.觉得有word就足够啦word可视化操作方便快捷. 但是由于要写论文等,在导师要求下潜心学习一下,不知不觉间被LaTeX的强大功能所吸引.现在很多出版 ...

  3. ftp 530 This FTP serveris anonymous only,

    引用:http://blog.sina.com.cn/s/blog_7e16680c01018ox1.html 三.遇到的问题 1.只允许匿名用户登录 现象:ftp连接过程中,提示输入用户名,输入本机 ...

  4. MyEclipse基础配置

    1.设置默认工作空间编码 window/preferences/general/workspace/Text file encoding 2.设置文件默认打开方式 xml建议设置 html建议设置 j ...

  5. 智能家居常用WiFi模块

    WiFi模块 WiFi模块就是整个系统的控制中心,控制很简单,就是输出一个开关信号控制继电器,而这个模块的核心是WiFi的连接,手机连接WiFi时需要扫描,输入密码,而这类本身没有屏幕和键盘的硬件设备 ...

  6. mysql复制表结构及检查表、存储过程是否存在

    mysql命令行复制表结构的方法: 1.只复制表结构到新表 CREATE TABLE 新表 SELECT * FROM 旧表 WHERE 1=2  或者 CREATE TABLE 新表 LIKE 旧表 ...

  7. db2常用命令(1)

    DB2常用命令   1.启动实例(db2inst1):实例相当于informix中的服务 db2start 2.停止实例(db2inst1): db2stop 3.列出所有实例(db2inst1) d ...

  8. [已解决] java.net.ConnectException: Connection refused: no further information

    程序抛出这个异常的原因多数是因为在此[host:port]没有监听,那么该如何解决这个问题呢,如下 第一个要做的是看你的host和port是否写错了,如 [ 127.00.1:8080 ] 第二个要看 ...

  9. 清除dns缓存

    Linux清除dns缓存命令 /etc/init.d/dnsmasq restart

  10. lua 操作中文字符串之截取和长度竖排显示

    前言 在游戏中,我们经常会遇到汉字的多行显示,比如名字竖行显示等.如下图: 为了实现上面的效果,lua实现分行是通过  \n  实现的,所以我们需要取出汉字,然后插入 \n 实现分行效果.还有一种就是 ...