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 ...
随机推荐
- Unity3D 系统宏
Platform Defines The platform defines that Unity supports for your scripts are: Property: Functi ...
- iOS,plist文件、pct文件,工程设置
1.使用pch文件 2.在info.plist中配置URL Schemes 3.plist配置拍照界面,复制,粘贴等菜单的显示语言 显示中文 4.使用非ARC库/ARC库 5.链接选项-Objc &a ...
- web server && web framework角色区分
问题 web framework是否包括webserver? 是否可以包括? webserver 和 framework的关系是? https://www.quora.com/What-is-the- ...
- cat常用参数详解
cat常用参数详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 最近,我的一个朋友对linux特别感兴趣,于是我觉得每天交给他一个命令的使用,这样一个月下来也会使用30个命令,基 ...
- Android APK反编译详解(附图)
转载自http://blog.csdn.net/sunboy_2050/article/details/6727581 这段时间在学Android应用开发,在想既然是用Java开发的应该很好反编译从而 ...
- jsp请求乱码问题
首先尝试添加filter,以下是我的自定义filter,实现了Filter接口: package com.deplume.ssm.filter;import javax.servlet.*;impor ...
- 编译openssl
windows: 下载openssl-1.0.1h.tar.gz文件 32位: 在解压的包中,有INSTALL.W32文件,按照文件提示安装 64位: 在解压的包中,有INSTALL.W64文件,按照 ...
- #if、#ifdef、#if defined之间的区别【转】
转自:http://quanminchaoren.iteye.com/blog/1870977 #if的使用说明 #if的后面接的是表达式 #if (MAX==10)||(MAX==20) code. ...
- JAVA下的Thread.sleep方法一定要try
try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } 不同于C#,JAVA里的Thre ...
- 如何清理photoshop cs6 被升级的烦人的adobe creative cloud组件
安装photoshop cs6(虽然目前已经退出到cc 2015,不过因激活成熟度等,我还是偏向于使用cs6,够用!),默认安装adobe application manager. 不过如果不小心单独 ...