Add two numbers [LeetCode]
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]的更多相关文章
- Add Two Numbers LeetCode Java
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- Add Two Numbers ---- LeetCode 002
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode 面试:Add Two Numbers
1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- leetcode 第二题Add Two Numbers java
链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- LeetCode:1. Add Two Numbers
题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...
随机推荐
- IBM服务器诊断面板
IBM服务器一般会有一个服务器操作员信息面板(诊断面板),服务器一般的硬件故障都会在诊断面板上提示,但这些提示可能只是一个大概的诊断故障,有助于系统管理员更好的维护. 一.IBM X3650 M3诊断 ...
- kafka 安装
kafka是一个分布式的消息缓存系统 kafka集群中的服务器都叫做broker kafka有两类客户端,一类叫producer(消息生产者),一类叫做consumer(消息消费者),客户端和brok ...
- Linux下安装APache
1:在图形界面下下载apache 安装包,我下的是 httpd-2.2.9.tar.gz 源码安装包,地址是http://httpd.apache.org/download.cgi 2:用:gzip ...
- Centos 安装 Java
建立文件夹 进入/usr/文件夹下,建立一个文件,我这里是java文件夹,将jdk-8u45-linux-x64.tar.gz复制到/usr/java文件夹下 解压文件 解压指令为:tar -zxvf ...
- 获取设置唯一的UDID的值
http://blog.sina.com.cn/s/blog_5971cdd00102vqgy.html ---方法 http://www.jianshu.com/p/a7a4a14c8030 -- ...
- VBA好的插件
VBE 小插件--- Office 编程助手 :http://club.excelhome.net/thread-1055425-1-1.html VB以及VBA的编程助手MZ-tools7.0破解版 ...
- 【转】C# Linq 交集、并集、差集、去重
using System.Linq; List<string> ListA = new List<string>(); List<string> ListB = n ...
- wex5 教程之 图文讲解 文件上传attachmentSimple(1)
视频教程地址:http://v.youku.com/v_show/id_XMTc4NDAyMTY4OA==.html 效果预览: 1 调用attchmentSimple组件,打开文件管理器,并选中,显 ...
- Linux多节点互信配置
SSH互信设置步骤: 1. 每个节点上分别生成自己的公钥和私钥 2. 将各节点的公钥文件汇总到一个总的认证文件authorized_keys中 3. 将这个包含了所有节点公钥的认证文件au ...
- MySQL and Postgres command equivalents (mysql vs psql)
MySQL and Postgres command equivalents (mysql vs psql) 博客分类: Database From: http://blog.endpoint.c ...