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 ...
随机推荐
- Typecho防黑安全加固-修改后台路径
删除安装文件 成功安装后删除install.php文件.install/文件夹. 修改后台地址 把admin修改为黑客猜不到的名字,例如pipixia,防止黑客穷举密码. 修改admin文件夹名称 修 ...
- 让Typecho支持Emoji表情,解决报错:Database Query Error
最近在使用一个主题时,看到搭配emoji表情可以让改主题更加美观,于是我就上了,结果在将emoji表情放进去保存的时候报错:Database Query Error,于是问起了度娘.最后的结果是: 在 ...
- 借Processing语言及IDE做DOS批处理的事务( 批量修改文件夹或文件的名字 )
一直想用Processing语言做一些批处理的事务,因为其自带的IDE功能紧凑易用,极度轻量,又加上Java语言的生态极具友好,处理一些windows相关操作完全可行,简单快捷. 这次就是用它做[批量 ...
- FastAPI路由与请求处理全解:手把手打造用户管理系统 🔌
title: FastAPI路由与请求处理全解:手把手打造用户管理系统 date: 2025/3/2 updated: 2025/3/2 author: cmdragon excerpt: 通过咖啡店 ...
- AI与.NET技术实操系列(四):使用Semantic Kernel和DeepSeek构建AI应用
引言 在人工智能技术飞速发展的今天,大型语言模型(Large Language Models, LLMs)已成为智能应用开发的核心驱动力.从智能客服到自动化内容生成,LLMs的应用正在深刻改变我们的工 ...
- [第三章]ABAQUS CM插件中文手册
ABAQUS Composite Modeler User Manual(zh-CN) Dassault Systèmes, 2018 注: 源文档的交叉引用链接,本文无效 有些语句英文表达更易理解, ...
- ORACLE SQL中执行先后次序的问题
分享一个经验 需求:Oracle中,根据COST优先级取最优先的一条记录脚本: select ... from ... where ... and rownum=1 order by cost 实际不 ...
- Django实战项目-学习任务系统-兑换物品管理
接着上期代码框架,开发第5个功能,兑换物品管理,再增加一个学习兑换物品表,主要用来维护兑换物品,所需积分,物品状态等信息,还有一个积分流水表,完成任务奖励积分,兑换物品消耗积分. 要想激励一个人的学习 ...
- IvorySQL 4.4 发布
IvorySQL 4.4 已于 2025 年 3 月 10 日正式发布.新版本全面支持 PostgreSQL 17.4,新增多项新功能,并修复了已知问题. 增强功能 PostgreSQL 17.3 增 ...
- Windows 提权-服务_弱注册表权限
本文通过 Google 翻译 Weak Registry Key Permissions – Windows Privilege Escalation 这篇文章所产生,本人仅是对机器翻译中部分表达别扭 ...