【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 ...
随机推荐
- Roy the Robber
Problem Description The aspiring Roy the Robber has seen a lot of American movies, and knows that th ...
- 2014.8.3情人节欢乐赛【Benny的农场】
Benny的农场 (farm.pas/.c/.cpp) 时间限制:1s.空间限制:128MB 题目描述: Benny有一片农田需要灌溉.农田的形状为矩形,并被分为许多小块.每一块中都有一些水管.共有1 ...
- Asp.Net的核心对象
原文地址:http://www.cnblogs.com/fish-li/archive/2011/08/21/2148640.html 1.HttpRuntime 对象在处理Http请求的asp.ne ...
- MyCat 介绍、分片规则、调优的内容收集
一.MyCat的简介 MyCat高可用.负载均衡架构图: 详细知识点: MySQL分布式集群之MyCAT(一)简介(修正) 二.MyCat的schema.xml讲解 详细知识点:MySQL分布式集群 ...
- 上传form表单
<form name="theForm" method="post" action="index.php?m=back&c=Goods& ...
- 让magento的validate验证hidden field
Object.extend(Validation, { isVisible : function(elm) { return true; }, insertAdvice : function(elm, ...
- ACdream OJ 1153 (k-GCD)
题目链接: http://115.28.76.232/problem?pid=1153 题意: 从给定的n个数中取出k个数,使得他们的最大公约数最大,求这个最大的公约数 分析: 暴力分解不可取,我们能 ...
- Win8 使用VC6.0调试
Win8.1下无法执行vc++6.0的解决方法 注意 安装过程中最后一步会卡在那里不动,能够直接关闭安装程序,忽略报错. 1 安装完毕后在安装文件夹下找到MSDEV.EXE 而且将 MSDEV.EXE ...
- 登录DSCCC控制台报错提示:安装错误代码: 3
登录DSCCC控制台报错内容:读取安装配置时出错 检查目录服务控制中心状态时出现意外错误. 显示详细资料 隐藏详细资料 安装错误代码: 3 堆栈: com.sun.directory.common.s ...
- ubuntu系统分区方案
一.各文件及文件夹的定义 /bin:bin是binary(二进制)的缩写.存放必要的命令 存放增加的用户程序. /bin分区,存放标准系统实用程序./boot:这里存放的是启动LINUX时使用的一些核 ...