[LeetCode]2. Add Two Numbers链表相加
注意进位的处理和节点为null的处理
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int flag = 0;
ListNode res = null;
ListNode temp = null;
//注意如果进位不是0的话还要添加一个节点
while (l1!=null||l2!=null||flag!=0)
{
int cur = flag;
flag = 0;
if (l1!=null) {
cur+=l1.val;
l1 = l1.next;
}
if (l2!=null) {
cur+=l2.val;
l2 = l2.next;
}
if (cur>9)
{
flag = 1;
cur%=10;
}
if (res==null)
{
res = new ListNode(cur);
temp = res;
continue;
}
temp.next = new ListNode(cur);
temp = temp.next;
}
return res;
}
[LeetCode]2. Add Two Numbers链表相加的更多相关文章
- leetcode 2 Add Two Numbers(链表)
数字反过来这个没有什么麻烦,就是镜像的去算十进制加法就可以了,然后就是简单的链表. /** * Definition for singly-linked list. * struct ListNode ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- LeetCode:1. Add Two Numbers
题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...
- [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 2. add two numbers && 单链表
add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...
- 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:基本加法规则 根据小学学的基本加法 ...
随机推荐
- Linux之【GNU】、【GPL】、【linux系统组成】
GNU,什么是GNU GNU全称:GNU's not unix GNU的重要组件(Emacs,gcc,bash,gawk等)加上自己的内核构成了GNU自己的系统--->没用 现在linux中的一 ...
- day4(cookie与session的原理及区别)
1.COOKIE使用和优缺点 1.1 cookie原理:用户名+密码 cookie是保存在用户浏览器端,用户名和密码等明文信息 1.2session使用原理 session是存储在服务器端的一段字符串 ...
- PyQt(Python+Qt)学习随笔:QTreeWidgetItem项的子项排序sortChildren及获取项对应的树型部件对象方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 1.sortChildren对子项排序 树型部件QTreeWidget中的QTreeWidgetIt ...
- Gradle上传依赖到私服(nexus)
子模块配置 buildscript { repositories { mavenLocal() maven { url "http://maven.aliyun.com/nexus/cont ...
- 算法——和为K的连续子数组
给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数. 输入:nums = [1,1,1], k = 2 输出: 2 , [1,1] 与 [1,1] 为两种不同的情况. 链 ...
- 微信小程序-卡券开发(前端)
刚完成一个微信小程序卡券开发的项目.下面记录开发前,自己困惑的几个问题. 因为我只负责了前端.所以下面主要是前端的工作. 项目概述:按照设计图开发好首页上的优惠券列表,点击某个优惠券,输入手机号,点击 ...
- 基于gin的golang web开发:实现用户登录
前文分别介绍过了Resty和gin-jwt两个包,Resty是一个HTTP和REST客户端,gin-jwt是一个实现了JWT的Gin中间件.本文将使用这两个包来实现一个简单的用户登录功能. 环境准备 ...
- 利用vs pcl库将多个PCD文件合并成一张PCD地图
主机环境:win10系统,pcl库1.11.1, vs2019 pcl库安装以及环境配置如下连接: https://www.jb51.net/article/190710.htm 代码很简单,主要是做 ...
- 博流BL602&BL604开发板介绍
在2020松山湖论坛上,博流智能科技(南京)有限公司销售副总裁刘占领介绍了基于RISC-V核的低功耗.高可靠Wi-Fi+BLE二合一SoC芯片BL602.主要应用领域包括人工智能与工业互联网,特别是电 ...
- antDesign中排序sorter的坑
antd中sorter是写在columns中的一个配置,结果为一个回调函数 如图,这是我项目中使用sorter的小例子,参数a,b分别为列表第0项数据和第1项数据.回调函数中return一个值,按照什 ...