[LeetCode_2] Add Two Numbers
LeetCode: 2. Add Two Numbers
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *addTwoNumbers( ListNode *l1, ListNode *l2 )
{
int carry = 0;// 是否需要进位
ListNode * tail = new ListNode(0);
ListNode * ptr = tail;// 当前指针
while(l1 != NULL || l2 != NULL){
int val1 = 0,val2 = 0;
if (l1 != NULL){
val1 = l1 -> val;
l1 = l1 -> next;
}
if (l2 != NULL){
val2 = l2 -> val;
l2 = l2 -> next;
}
ptr -> val = (val1 + val2 + carry) % 10 ;
carry = ( val1 + val2 + carry )/ 10;
// 增加
if (l1 != NULL || l2 != NULL){
ptr -> next = new ListNode(0);
ptr = ptr -> next;
}
}
if (carry == 1){
ptr -> next = new ListNode(1);
}
return tail;
}
};
[LeetCode_2] Add Two Numbers的更多相关文章
- [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 ...
- 52. 不用+、-、×、÷做加法[add two numbers without arithmetic]
[本文链接] http://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html [题目] 写一个函数,求两个整数的 ...
- Leetcode-2 Add Two Numbers
#2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...
- [CareerCup] 2.5 Add Two Numbers 两个数字相加
2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ...
- [LintCode] Add Two Numbers 两个数字相加
You have two numbers represented by a linked list, where each node contains a single digit. The digi ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- Two Sum & Add Two Numbers
Two Sum 题目:https://leetcode.com/problems/two-sum/ class Solution(object): def twoSum(self, nums, tar ...
- No.002 Add Two Numbers
Add Two Numbers Total Accepted: 160702 Total Submissions: 664770 Difficulty: Medium You are given tw ...
随机推荐
- RGB转YCbCr
从RGB转换成YCbCr // Purpose: // Save RGB->YCC colorspace conversion for reuse, only co ...
- 2.4.1 用NPOI操作EXCEL--画线(转)
之所有说NPOI强大,是因为常用的Excel操作她都可以通过编程的方式完成.这节开始,我们开始学习NPOI的画图功能.先从最简单的开始,画一条直线:对应的代码为: HSSFSheet sheet1 = ...
- spark 源码安装
clone 源码 git clone git://github.com/apache/spark.git maven编译源码 国外镜像比较慢,此处修改maven仓库的镜像为阿里云镜像: <mir ...
- 设置文本框左边显示的View
效果:
- Windows下使用VisualSVN Server搭建SVN服务器
使用 VisualSVN Server来实现主要的 SVN功能则要比使用原始的 SVN和 Apache相配合来实现源代码的 SVN管理简单的多,下面就看看详细的说明. VisualSVN Server ...
- Web页面中5种超酷的Hover效果
hover 效果能给网页增加一些动态效果,并且使得站点更具有活力.原来的做法是使用javascript来实现这些动态效果,但是随着CSS3的引入和现代浏览器 的支持,我们可以用纯粹的CSS代码来实现这 ...
- JSP基本语法小结
jsp表达式:<%=???%> 在jsp页面嵌入java代码<%Java代码%>可以用多个<% %>分割代码段 jsp声明:<%!用这样的方法可以声明java ...
- 解读ContentResolver和ContentProvider
转自:http://cthhqu.blog.51cto.com/7598297/1281217 1. ContentProvider的概述 ContentProvider: (Official Def ...
- [转]IntelliJ IDEA 使用心得与常用快捷键
IntelliJ IDEA 使用心得与常用快捷键 那种酸爽,根本说不出来—————————————————————————— by: Jimi没有BondJimi是谁? 就是洒家啊! 刚开始学习写Ja ...
- EL表达式查询出来的数据,下载成excel表格,很实用的
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...