Leetcode-2 Add Two Numbers
#2. Add Two Numbers
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
/**
* 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) {
ListNode* head=new ListNode();
ListNode* p=head;
ListNode* q=head; int carry=;
int sum=;
int all=;
while(l1!=NULL&&l2!=NULL)
{
all=l1->val+l2->val+carry;
sum=all%;
carry=all/;
p->val=sum;
p->next=new ListNode();
p=p->next;
l1=l1->next;
l2=l2->next;
} while(l1!=NULL)
{
all=l1->val+carry;
sum=all%;
carry=all/;
p->val=sum;
p->next=new ListNode();
p=p->next;
l1=l1->next;
}
while(l2!=NULL)
{
all=l2->val+carry;
sum=all%;
carry=all/;
p->val=sum;
p->next=new ListNode();
p=p->next;
l2=l2->next;
}
if(carry!=)
{
p->val=;
p->next=new ListNode();
p=p->next;
}
while(q!=NULL)
{
if(q->next->next==NULL)
{
q->next=NULL;
break;
}
q=q->next;
} return head; }
};
/**
* 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) {
ListNode* head=new ListNode();
ListNode* p=head; int carry=;
int sum=;
int all=;
while(l1!=NULL&&l2!=NULL)
{
all=l1->val+l2->val+carry;
sum=all%;
carry=all/;
p->next=new ListNode(sum);
p=p->next;
l1=l1->next;
l2=l2->next;
} while(l1!=NULL)
{
all=l1->val+carry;
sum=all%;
carry=all/;
p->next=new ListNode(sum);
p=p->next;
l1=l1->next;
}
while(l2!=NULL)
{
all=l2->val+carry;
sum=all%;
carry=all/;
p->next=new ListNode(sum);
p=p->next;
l2=l2->next;
}
if(carry!=)
{
p->next=new ListNode();
p=p->next;
} return head->next; }
};
Leetcode-2 Add Two Numbers的更多相关文章
- 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 ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- LeetCode 面试:Add Two Numbers
1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- LeetCode #002# Add Two Numbers(js描述)
索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...
- [Leetcode Week15] Add Two Numbers
Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
- [LeetCode] 2. Add Two Numbers 两个数字相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- LeetCode之Add Two Numbers
Add Two Numbers 方法一: 考虑到有进位的问题,首先想到的思路是: 先分位求总和得到 totalsum,然后再将totalsum按位拆分转成链表: ListNode* addTwoNum ...
- LeetCode 2. add two numbers && 单链表
add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...
随机推荐
- 本地xdebug调试搭建 Laravel+homestead+phpstorm
1.在homestead virtual box安装和配置xdebug 先在终端运行vagrant up 和 vagrant ssh,ssh远程到homestead,然后复制以下代码到一个shell文 ...
- SDOI2009
1226: [SDOI2009]学校食堂Dining Description 小F 的学校在城市的一个偏僻角落,所有学生都只好在学校吃饭.学校有一个食堂,虽然简陋,但食堂大厨总能做出让同学们满意的菜肴 ...
- SRM 628 DIV2
250 想想就发现规律了. 500 暴力,括号匹配. 1000 给一个f数组,如果i存在,那么f[i]也得存在,问这样的集合有多少种. 先拓扑一下,dp[i] = mul(dp[son]+1)最后 ...
- Day 1:开始重新学习
离开很久,前几天翻出以前做过的程序居然还能正常运行.有一点后悔,为什么当初没有坚持做下去.Delphi园地前一阵也曾经宣布要关站,但仍然坚持过来了,在此向站长致敬!我也要重新开始! 附图:Delphi ...
- src/main/Java路径下的properties文件丢失
在pom中加入代码: <build> <resources> <resource> <directory>src/main/java</direc ...
- UbuntuでPostgreSQLをインストールからリモートアクセスまでの手順
PostgreSQLサーバの立ち上げに少しハマりましたので.メモしておきます. OS: Ubuntu14.04 LTS インストール 最初はPostgreSQLをインストールします.普通にapt-ge ...
- bat 延时删除指定文件夹中的文件经验分享
1.bat延时 xp程序中通过ping 127.0.0.1 -n 20 来实现延时操作,ping本地地址20行. win7中通过timeout 20 来实现延时20秒. 2.删除指定文件 del /q ...
- OPENDATASOURCE读取远程数据库数据中文乱码问题-sqlserver R2
insert into kraft_sync_Store(StoreName,StoreCode,Province,PrefectureCity,CountyCity,Region,Area,Unit ...
- 解读2015年互联网UGC内容发展态势,安全事件频发
<2015内容安全年报> 阿里移动安全 第一章 2015年内容安全形势 随着互联网业务的迅速发展,互联网上的信息内容带来了爆炸式的增长.由于缺乏对网络活动进行有效监督和管理的措施,致使互联 ...
- 浅谈WEB安全性(前端向)
相信进来的时候你已经看到alert弹窗,显示的是你cookie信息(为配合博客园要求已删除).单纯地在你的客户端弹出信息只是类似于迫使你在自己的房间脱衣服——没人看得到,自然也不算啥恶意行为.那么如果 ...