当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
Do self-media monthly income tens of thousands?Several self-media tools that bloggers are using
Flutter气泡框实现
[Go WebSocket] 多房间的聊天室(一)思考篇
模块九 - 设计电商秒杀系统
CPU多级缓存与缓存一致性
WeChat applet, global variables change in one place and the state in other places also changes.
Network sockets (UDP and TCP programming)
ViT结构详解(附pytorch代码)
网络基础(第一节)
Analysis of the implementation principle of UUID from the perspective of source code
随机推荐
做自媒体月入几万?博主们都在用的几个自媒体工具
力扣练习——64 最长和谐子序列
std::move()
codevs 2370 小机房的树 (LCA)
接口定义与实现
使用哈工大LTP测试分词并且增加自定义字典
英特尔推送20220809 CPU微码更新 修补Intel-SA-00657安全漏洞
建校仅11年就入选“双一流” ,这所高校是凭什么做到的?
LeetCode 138. 复制带随机指针的链表
L2 applications from a product perspective: why is it a playground?
LeetCode 86. 分隔链表
OPNsense安装配置Zenarmor
Clicking Exercise - 64 Longest Harmonic Subsequences
StoneDB 文档捉虫活动第一季
[Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
The brave rice rice, does not fear the brush list of 】 list has a ring
基于UiAutomator2+PageObject模式开展APP自动化测试实战
A case of violent parameter tuning in machine learning
推荐6个自媒体领域,轻松易上手
Centos7 environment uses Mysql offline installation package to install Mysql5.7