当前位置:网站首页>LeetCode 369. Plus One Linked List
LeetCode 369. Plus One Linked List
2022-08-10 11:57:00 【Mizuna pen】
给一个链表,represents a number such as123;Indicates yes with a linked list1->2->3.Now add one to this number and return the linked list structure;
public ListNode reverseList(ListNode node) {
if (node.next == null) {
return node;
}
ListNode result = reverseList(node.next);
node.next.next = node;
node.next = null;
return result;
}
public ListNode plusOne(ListNode head) {
if (head == null) {
return head;
}
// 先翻转,In this way, the single digits can be traversed first
ListNode reverseNode = reverseList(head);
ListNode cur = reverseNode;
int mod = 0;
int add = 1;
while (cur != null) {
// 遍历每一个节点,
int sum = cur.val + add + mod;
cur.val = sum % 10;
mod = sum / 10;
add = 0;
cur = cur.next;
}
// There is no rush to splice nodes where the remainder is pure,Anyway, this node is also the first,先翻转链表,Then stitch it together.
ListNode result = reverseList(reverseNode);
if (mod != 0) {
ListNode node = new ListNode(1);
node.next = result;
return node;
}
return result;
}
边栏推荐
- codevs 2370 Small room tree (LCA)
- 不止跑路,拯救误操作rm -rf /*的小伙儿
- 不止跑路,拯救误操作rm -rf /*的小伙儿
- 孩子自律性不够?猿辅导:计划表要注意“留白”给孩子更多掌控感
- HDU 6040 Hints of sd0061 (技巧)
- Hangdian Multi-School-Loop-(uncertainty greedy + line segment tree)
- LAXCUS分布式操作系统安全管理
- Article take you understand interrupt the key driver of polling mechanism
- 使用.NET简单实现一个Redis的高性能克隆版(六)
- 三星计划2023年开始在越南生产半导体零部件
猜你喜欢
How many constants and data types do you remember?
为什么Redis很快
ENVI 5.3软件安装包和安装教程
Article take you understand interrupt the key driver of polling mechanism
老板加薪!看我做的WPF Loading!!!
ViT结构详解(附pytorch代码)
StoneDB 文档捉虫活动第一季
Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇
How to join We Media, learn about these 5 monetization modes, and make your account quickly monetize
Module 9 - Designing an e-commerce seckill system
随机推荐
孩子自律性不够?猿辅导:计划表要注意“留白”给孩子更多掌控感
软件架构简介
Since the media hot style title how to write?Taught you how to write the title
LeetCode50天刷题计划(Day 16—— 两两交换链表中的节点(9.10-10.30)
POJ 1026 Cipher (Permutation Groups)
老板加薪!看我做的WPF Loading!!!
三星计划2023年开始在越南生产半导体零部件
Not just running away, but saving the guy who mishandled rm -rf /*
Analysis of the implementation principle of UUID from the perspective of source code
机器学习之暴力调参案例
Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇
【Untitled】
使用JMeter进行MySQL的压力测试
[Go WebSocket] 多房间的聊天室(一)思考篇
从脚本到剪辑,影像大师亲授的后期制作秘籍
codevs 2370 Small room tree (LCA)
【LeetCode】640. 求解方程
What are some useful performance testing tools recommended? Performance testing report charging standards
Licking Exercise - 58 Verifying Binary Search Trees
ssm框架搭建过程[通俗易懂]