21-Add Two Numbers-Leetcode
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
思路:从左边对齐,开始相加,将产生的进位置为下一个指针的初始值,这里主要需要考虑到几种情况,两个链表相等或不等两类,
最重要的是根据进位决定next指针是否为空或者是一个新节点。
/**
* 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(0);
ListNode * beg = head;
int pre=0;
while(l1!=NULL||l2!=NULL){
if(l1!=NULL&&l2!=NULL){
head->val = head->val+l1->val+l2->val;
pre = head->val/10;
head->val = (head->val)%10;
l1 = l1->next;
l2 = l2->next;
if(l1==NULL&&l2==NULL&&pre==0)head->next = NULL;//这个if很重要
else head->next = new ListNode(pre);
head = head->next;
}
else if(l1!=NULL&&l2==NULL){
head->val = head->val+l1->val;
pre = head->val/10;
head->val = (head->val)%10;
l1 = l1->next;
if(l1==NULL&&pre==0)head->next = NULL;
else head->next = new ListNode(pre);
head = head->next;
}
else if(l1==NULL&&l2!=NULL)
{
head->val = head->val+l2->val;
pre = head->val/10;
head->val = (head->val)%10;
l2 = l2->next;
if(l2==NULL&&pre==0)head->next = NULL;
else head->next = new ListNode(pre);
head = head->next;
}
else{
head->val = pre;
return beg;
}
}
return beg;
}
};
21-Add Two Numbers-Leetcode的更多相关文章
- Add Two Numbers LeetCode Java
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- Add two numbers [LeetCode]
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- Add Two Numbers ---- LeetCode 002
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode 面试:Add Two Numbers
1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- leetcode 第二题Add Two Numbers java
链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...
- 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 ...
随机推荐
- 2021.8.19考试总结[NOIP模拟44]
T1 emotional flutter 把脚长合到黑条中. 每个黑条可以映射到统一区间,实际操作就是左右端点取模.长度大于$k$时显然不合法. 然后检查一遍区间内有没有不被黑条覆盖的点即可. 区间端 ...
- 主集天线和分集天线——4G天线技术
主集天线和分集天线 分集接收技术是一项主要的抗衰落技术,可以大大提高多径衰落信道传输下的可靠性,在实际的移动通信系统中,移动台常常工作在城市建筑群或其他复杂的地理环境中,而且移动的速度和方向是任意的. ...
- MyBatis源码分析(五):MyBatis Cache分析
一.Mybatis缓存介绍 在Mybatis中,它提供了一级缓存和二级缓存,默认的情况下只开启一级缓存,所以默认情况下是开启了缓存的,除非明确指定不开缓存功能.使用缓存的目的就是把数据保存在内存中,是 ...
- 文献翻译|Design of True Random Number Generator Based on Multi-stage Feedback Ring Oscillator(基于多级反馈环形振荡器的真随机数发生器设计)
基于多级反馈环形振荡器的真随机数发生器设计 摘要 真随机数生成器(trng)在加密系统中起着重要的作用.本文提出了一种在现场可编程门阵列(FPGA)上生成真随机数的新方法,该方法以 多级反馈环形振荡器 ...
- P2774 方格取数问题(最小割)
P2774 方格取数问题 一看题目便知是网络流,但由于无法建图.... 题目直说禁止那些条件,这导致我们直接建图做不到,既然如此,我们这是就要逆向思维,他禁止那些边,我们就连那些边. 我们将棋盘染色, ...
- cf17A Noldbach problem(额,,,素数,,,)
题意: 判断从[2,N]中是否有超过[包括]K个数满足:等于一加两个相邻的素数. 思路: 枚举. 也可以:筛完素数,枚举素数,直到相邻素数和超过N.统计个数 代码: int n,k; int prim ...
- 记录 mysql 使用时遇到的问题
1,linux平台上mysqld和mysql的区别. 首先,mysql数据库是标准的c/s架构,yum安装时注意到了,有mysql和mysql-server包 mysql是客户端的工具,mysqld ...
- ssl 原理和建立连接过程
ssl ("Secure Sockets Layer")加密原理 和https的关系https = http + ssl ssl 位置: SSL握手 证书主要作用是在SSL握手中, ...
- TDengine在浙商银行微服务监控中的实践
作者:楼永红 王轩宇|浙商银行 浙商银行股份有限公司(简称"浙商银行")是 12 家全国性股份制商业银行之一,总部设在浙江杭州,全国第13家"A+H"上市 ...
- Java日期API
JDK8之前日期时间API java.util.Date类 表示特定的瞬间,精确到毫秒 构造器: Date():使用无参构造器创建的对象可以获取本地当前时间. Date(long date) 常用方法 ...