[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 ...
随机推荐
- 《linux内核设计与实现》读书笔记第十七章
第17章 设备与模块 四种内核成分 设备类型:在所有 Unix 系统中为了统一普通设备的操作所采用的分类. 模块: Linux 内核中用于按需加载和卸载目标码的机制. 内核对象:内核数据结构中支持面向 ...
- C# 测试服务器连接 Ping
.aspx页: 一个textbox(txtIP)输入服务器地址,一个button(Btn_ok)点击测试,一个listbox(lboxContent)显示测试信息 .aspx.cs页: using S ...
- ui-grid
html代码: <html ng-app="myApp"> <head> <meta charset="utf- ...
- MongoDB的安装和配置成服务的三种方法和一些难点
1. Hotfix KB2731284 or later update is not installed的问题: If you are running any edition of Windows S ...
- sqlalchemy mark-deleted 和 python 多继承下的方法解析顺序 MRO
sqlalchemy mark-deleted 和 python 多继承下的方法解析顺序 MRO 今天在弄一个 sqlalchemy 的数据库基类的时候,遇到了跟多继承相关的一个小问题,因此顺便看了一 ...
- ios结构体语法
- RabbitMQ模拟器使用方法
同事推荐了一个很好玩的RabbitMQ模拟器,记录一下简要使用方法. 模拟器地址 http://tryrabbitmq.com/ 使用方法: 直接将画框左面的图标拖进画图区,构建想要的拓扑图: 按住A ...
- Intellij IDE 常用设置
1· 去除代码提示的Case sensitive(比如Sprite,键入sprite不进行任何提示) Editor->Code Completion->Case sensitive com ...
- 添加系统右键菜单项 管理员取得所有权(W)(带盾牌)
@color 0A @title 添加系统右键菜单项 管理员取得所有权(^&W)(带盾牌) by wjshan0808 @echo off echo * >nul reg add HKC ...
- C# 文件压缩加解密
1.这种方式也可以做到对文件的加密压缩,解密解压缩,只是在压缩和解压缩时会出现压缩窗口 1.1加密压缩 strzipPath:压缩包路径 strtxtPath:待压缩的文件路径 password:加密 ...