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 ...
随机推荐
- Noip模拟54 2021.9.16
T1 选择 现在发现好多题目都是隐含的状压,不明面给到数据范围里,之凭借一句话 比如这道题就是按照题目里边给的儿子数量不超过$10$做状压,非常邪门 由于数据范围比较小,怎么暴力就怎么来 从叶子节点向 ...
- 震惊,hzoi的考试竟然折磨简单,活到爆!
众所周知,hzoi的考试题非常"简单",那么究竟有多简单呢?最近,一位外国小哥开发出了hzoi的考试竟然折磨简单,活到爆!的方法,这究竟是怎么一回事呢?快和小编一起来看看吧- 满分 ...
- BZOJ4919[Lydsy1706月赛]大根堆-------------线段树进阶
是不是每做道线段树进阶都要写个题解..根本不会写 Description 给定一棵n个节点的有根树,编号依次为1到n,其中1号点为根节点.每个点有一个权值v_i. 你需要将这棵树转化成一个大根堆.确切 ...
- Vue | uni-app 中使用websocket
@ 目录 首先在根目录下新建一个store文件夹,并新建一个websocket.js文件,代码如下: import Vue from 'vue' import Vuex from 'vuex' Vue ...
- arduino 使用 analogRead 读取不到数据,digitalRead 却可以正常读取
项目场景: 最近在使用安信可的 ESP32S P14 引脚(ADC 16)读取一个电路状态的时候遇到一个问题,电路状态不是很稳定,在高电平的时候,会突然出现毫秒级的波动,出现短暂的低电平,造成设备状态 ...
- hdu 3038 How Many Answers Are Wrong(并查集)
题意: N和M.有N个数. M个回答:ai, bi, si.代表:sum(ai...bi)=si.如果这个回答和之前的冲突,则这个回答是假的. 问:M个回答中有几个是错误的. 思路: 如果知道sum( ...
- 面试题系列:工作5年,第一次这么清醒的理解final关键字?
面试题:用过final关键字吗?它有什么作用 面试考察点 考察目的: 了解面试者对Java基础知识的理解 考察人群: 工作1-5年,工作年限越高,对于基础知识理解的深度就越高. 背景知识 final关 ...
- BQ40Z50固件怎么升级?告诉你BQ系列芯片内部结构和升级方法
一 BQ芯片初步认识 包括BQ40Z50在内,BQ系列电池管理芯片看起来是一个芯片,其实芯片里面封装了两个die.一个是MCU部分负责计算和控制,其采用的是bqBMP内核的16位处理器:另外一个die ...
- Typecho部署小破站
写在前面 以前利用 Github Page + Hexo框架 + Next主题搭建过静态博客,没错就是那个黑白色系的网页!但是体验并不是很好,一来本身是静态网页,页面内容要修改都需要在本地修改完上传到 ...
- httprunner3源码解读(1)简单介绍源码模块内容
前言 最近想着搭建一个API测试平台,基础的注册登录功能已经完成,就差测试框架的选型,最后还是选择了httprunner,github上已经有很多开源的httprunner测试平台,但是看了下都是基于 ...