Plus One Linked List -- LeetCode
2024-09-04 18:01:27
Given a non-negative number represented as a singly linked list of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
Example:
Input:
->-> Output:
->->
思路:递归。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
int help(ListNode* head) {
if (head->next == NULL) head->val++;
else head->val += help(head->next);
if (head->val < ) return ;
head->val = ;
return ;
}
ListNode* plusOne(ListNode* head) {
int credit = help(head);
if (!credit) return head;
ListNode *res = new ListNode();
res->next = head;
return res;
}
};
最新文章
- 【MSSQL】MSSQL还原单mdf文件报1813错误
- git submodule 使用
- PChar,PAnsiChar,String,AnsiString,Char数组,AnsiChar数组转换
- English idioms
- NIO组件Selector工作机制详解(上)
- Spring入门导读——IoC和AOP
- flask 扩展之 -- flask-pagedown
- 【java】java.util.regex.Pattern和java.util.regex.Matcher简单示例
- 【bird-java】分布式服务间的事件总线EventBus
- windows平台上用python 远程线程注入,执行shellcode
- Java+Selenium操作日期时间选择框插件
- RMQ算法区间最值
- C# 引用的程序集没有强名称
- jQuery 重复加载,导致依赖于 jQuery的JS全部失效问题
- C# 调用 taskkill命令结束服务进程
- SQLServer 之 树查询
- 技嘉主板+AMD CPU开启CPU虚拟化方法
- idea+spring+springmvc+mybatis+mybatis+maven
- JSP页面退出时清除会话Session
- Debian 安装Linux源码