【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 ...
随机推荐
- Linux系统编程(21)——信号的产生
1.通过终端按键产生信号 通过上一篇我们知道了SIGINT的默认处理动作是终止进程,SIGQUIT的默认处理动作是终止进程并且Core Dump,现在我们来验证一下. 首先解释什么是Core Dump ...
- bootstrap绿色大气后台模板下载[转]
From:http://www.oschina.net/code/snippet_2364127_48176 1. [图片] 2. [文件] 素材火官网后台模板下载.rar ~ 4MB 下载( ...
- 有序线性搜索(Sorted/Ordered Linear Search)
如果数组元素已经排过序(升序),那我们搜索某个元素就不必遍历整个数组了.在下面给出的算法代码中,到任何一点,假设当前的arr[i]值大于搜索的值data,就可以停止搜索了. #include<s ...
- 剑指offer-面试题15.链表中倒数第k个结点
题目:输入一个链表,输出该链表的倒数第K个结点.为了符合大多数人的习惯,本题 从1开始计数,即链表的尾结点是倒数第1个节点.例如有一个链表有6个节点,从 头节点开始他们的值依次是1,2,3,4,5,6 ...
- A canvas fillText and strokeText example
A canvas fillText and strokeText example A canvas fillText and strokeText example
- leetcode_question_73 Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow ...
- Yaffs文件系统简介
1 简介 1.1 应用场合 Yaffs(Yet Another Flash File System)文件系统是专门针对NAND闪存设计的嵌入式文件系统,目前有YAFFS和YAFFS2两个版本,两个版本 ...
- Properties的读取和写入
Properties是HashTable下的一个持久的属性集,没有泛型,key-value都是String类型.由于能与IO流结合使用,所以能方便地操作属性文件或者xml文件. 一.propertie ...
- HTML中的figure与figcaption标签
本来想分两篇文章来解释说明figure.figcaption的,但是这俩个标签都是定义图文的,所以我们合起来讲解,大家更能容易接受. 大家在写xhtml.html中常常用到一种图片列表,图片+标题或者 ...
- ios ColorLUT滤镜
通过这种方格图片实现滤镜 代码: "CIFilter+ColorLUT.h" "CIFilter+ColorLUT.m" #import "CIFil ...