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 ...
随机推荐
- C练习
int main() { int a; //scanf函数只接受变量的地址 //scanf函数是一个阻塞式的函数,等待用户输入 // 用户输入完成后,就会将用户输入的值赋值给变量a //函数调用完毕 ...
- Navicat备份远程Oracle数据库到本地
公司的数据库是本地的,我只能在公司连,回家就不能跑项目了,一跑就报SQLException,所以希望可以把数据库复制到我的本地来. 因为一直在用Navicat操作数据库,这里就分享一下用Navicat ...
- 帝国CMS如何自动生成sitemap.xml网站地图文件
登录网站的后台http://你的域名/e/admin/ 进入后台栏目 =>增加自定义页面 =>选择直接页面,页面名称为:网站地图,文件名修改为 ../../sitemap.xml 内容填 ...
- BZOJ3197 & 组合乱搞
Description 求\[\sum_{i = 1}^{n}i^m m^i , m \leq 1000 \] 的值.Solution From Miskcoo's Space: ...
- USER STORIES AND USE CASES - DON’T USE BOTH
We’re in Orlando for a working session as part of the Core Team building BABOK V3 and over dinner th ...
- homework-01
我的GitHub账户名是Firedamp. 其实我最一开始看到最大子序列的和这个题目,最先想到的就是最简单的O(n^3)的算法,在课堂上教的也确实是这个程序,但是这种算法的时间复杂度必然是最高的,在数 ...
- Ibatis 使用心得
(1)建表语句 sqlMap: <statement id="createTable" parameterClass="String" > $sql ...
- 浏览器全屏事件(Html5)
<button onclick="launchFullscreen(document.documentElement);"></button> functi ...
- angularJs 指令的理解
指令: 可以把指令理解成为特定的DOM 上运行的函数,指令扩展了这个元素的功能: 例如:ng-click--在DOM元素上绑定了一个click事件的监听,并在监听到事件时运行angular表达式 自定 ...
- PreparedStatement的应用
package it.cast.jdbc; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql ...