Add Two Numbers--LeetCode进阶路②
- 题目描述:
You are given two non-empty linked lists representing two non-negative integers. 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.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
- 思路分析:依旧温柔风的题儿~建个新链表,把两个链表求和撸一遍,需要注意求和时的进位问题
- 代码实现:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1 == null || l2 == null){
return l1 != null ? l1:l2;
} int sum = l1.val + l2.val;
ListNode result = new ListNode(sum % 10);
result.next = addTwoNumbers(l1.next,l2.next); if(sum >= 10){//需要进位的情况
result.next = addTwoNumbers(result.next,new ListNode(sum / 10));
} return result;
}
}#现在的方法相当粗暴,现在给自己的目标是每天一题(并没有什么质量的那种啊啊啊),小陌考完四级要好好想想如何优化刷的题
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 445. 两数相加 II(Add Two Numbers II)
445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个 ...
- [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 ...
随机推荐
- 使用PySide6/PyQt6实现Python跨平台GUI框架的开发
在前面的<Python开发>中主要介绍了FastAPI的后端Python开发,以及基于WxPython的跨平台GUI的开发过程,由于PySide6/PyQt6 在GUI的用途上也有很大的优 ...
- Flink学习(十一) Sink到Elasticsearch
导入依赖 <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-conn ...
- C++ open()和read()函数使用详解
对于Framework工程师来说,必要C或者C++编程能力是必须的,像对设备节点的操作是最基本的操作,那么我们便会用到open和read函数.open()函数用于打开文件,而read()函数用于从打开 ...
- next.js 添加 PWA 渐进式WEB应用(service-worker) 支持
本文仅作为 next 系列文章中的一部分,其他 next 文章参考: https://blog.jijian.link/categories/nextjs/ 去 github 搜索了一把,估计是我关键 ...
- 关于TFDMemtable的使用场景【2】处理SOAP/REST取得的数据
如果可以直接获得JSON数据,那么可以直接连到TFDMemtable进行显示和编辑. 1.一组REST组件.RESTClient的属性BaseURL是http地址. 2.点击TRESTRequest右 ...
- Win10资源管理器导航窗格显示/隐藏项目-注册表
一.隐藏快速访问 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer HubMode(类型:REG_DWORD) ...
- 时区转换工具+PWA离线网页
时区转换工具+PWA离线网页 一.时区转换工具对比 工具 说明 Date 原生 JS API,有限的时区支持,无法指定时区,仅使用本地时区. Intl.DateTimeFormat 原生格式化显示,可 ...
- JAVA基础之多线程一期
一.并发与并行的区别 并发:指同一时间段,两个或多个事件交替进行 并行:指同一时间段,两个或多个事件同时进行 二.进程与线程的区别 进程:正在内存中运行的程序就是进程 线程:线程归属于进程,它是进程中 ...
- eolinker响应预处理:返回结果内循环读取同类数据,设置为变量
特别注意:需要使用全局变量或者预处理前务必阅读本链接https://www.cnblogs.com/becks/p/13713278.html 场景描述: 删除(清空)购物车接口,需要传入获取的每一项 ...
- MySQL安装入门第一篇
[1]MySQL的版本:近期主要历史版本有5.0/5.1/5.5/5.6/5.7,目前最新版本是MySQL8.6.0曾经是个内部试验版本,已取消了. MySQL8.0的版本历史 1) 2016-09- ...