【LeetCode练习题】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
题目意思:
给定两个链表,返回其相加的结果。注意链表是倒序的,即第一个节点是个位。
解题思路:
这个有点类似于大数加法,只不过大数加法用的是vector,可以求出长度,而链表则不行,而且链表还要小心空指针。
加法过程就是小学加法的那个过程,对应位相加并且加上上一位的进制。因为链表长短不一,所以当某一个链表结束而另外一个没结束时,需要让没结束的那个链表的每个节点继续和0相加即可。
这题没什么难度哈……
代码如下:
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
if(!l1)
return l2;
if(!l2)
return l1;
ListNode *head = new ListNode();
ListNode *ret = head;
ListNode *p = head;
int nCarry = ;
while(l1 && l2){
int a = l1->val;
int b = l2->val;
int c = a + b + nCarry;
nCarry = c / ;
// c % 10 new node
p->next = new ListNode(c%);
p = p->next;
l1 = l1->next;
l2 = l2->next;
}
//当l1还有剩余时,用l1里的节点继续和0加
while(l1){
int a = l1->val;
int b = ;
int c = a + b + nCarry;
nCarry = c / ;
p->next = new ListNode(c%);
p = p->next;
l1 = l1->next;
}
//当l2还有剩余时,用l2里的节点继续和0加
while(l2){
int a = l2->val;
int b = ;
int c = a + b + nCarry;
nCarry = c / ;
p->next = new ListNode(c%);
p = p->next;
l2 = l2->next;
}
//类似于 1 -> 9和 2 -> 1相加这种,需要新加一个值为1的节点。
if(nCarry){
p->next = new ListNode();
nCarry = ;
}
ret = ret->next;
delete head;
return ret;
}
};
【LeetCode练习题】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 ...
随机推荐
- nginx监听端口和反向代理端口的权限问题
Linux的SELinux安全性控制除作用于文件系统外还作用于端口,这使得那些作为服务启动的进程只能在规定的几个端口上监听.为叙述方便我们称之为受控端口. nginx监听端口 要查看当前有哪些受控端口 ...
- LeeCode-Insertion Sort List
Sort a linked list using insertion sort. /** * Definition for singly-linked list. * struct ListNode ...
- Unity position和localposition
1. position是根据世界原点为中心 2. localPosition是根据父节点为中心,如果没有父节点,localpositon和position是没有区别的 3.选中一个物体左上角Globa ...
- 安装jansson库【JSON库C语言版】
本次操作在Ubuntu 14.04下进行,其他的系统大同小异,安装软件时请根据系统版本进行调整. 1.下载jansson源码: git clone https://github.com/akheron ...
- org.springframework.web.util.IntrospectorCleanupListener的用途
Spring官方API中对其描述如下 /** * Listener that flushes the JDK's {@link java.beans.Introspector JavaBeans In ...
- ComponentName的意思
ComponentName是用来打开其它应用程序中的Activity或服务的. 使用方法: Intent i=new Intent(); i.setComponent(new ComponentNam ...
- [Uva247][Tarjan求强连通分量][Calling Circles]
题目大意: 例如:A跟B打电话,B跟C打电话,C跟A打电话..D跟E打电话,E跟D不打电话.则A,B,C属于同一个电话圈,D,E分别属于一个电话圈,问有多少个电话圈. 分析 就是裸的求强连通分量,直接 ...
- 【枚举+数学】【HDU1271】整数对 难度:五颗星
整数对 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- .NET Printer(打印)详细分析
如果你是一名开发人员,就不应该不知道.NET Framework.它的作用在编程人员眼中是非常强大的..NET Framework打印功能都以组件的方式提供,为程序员提供了很大的方便,但是这几个组件的 ...
- relativelayout常用属性
===================================================================================== 整理于http://naot ...