Plus One Linked List -- LeetCode
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;
}
};
Plus One Linked List -- LeetCode的更多相关文章
- Palindrome Linked List leetcode
Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...
- Flatten Binary Tree to Linked List [LeetCode]
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- Flatten Binary Tree to Linked List (LeetCode #114 Medium)(LintCode #453 Easy)
114. Flatten Binary Tree to Linked List (Medium) 453. Flatten Binary Tree to Linked List (Easy) 解法1: ...
- Flatten Binary Tree to Linked List ——LeetCode
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- Linked List - leetcode
138. Copy List with Random Pointer //不从head走 前面加一个dummy node 从dummy走先连head 只需记录当前节点 //这样就不需要考虑是先new ...
- Delete Node in a Linked List leetcode
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- Flatten Binary Tree to Linked List leetcode java
题目: Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 ...
- 876. Middle of the Linked List - LeetCode
Question 876. Middle of the Linked List Solution 题目大意:求链表的中间节点 思路:构造两个节点,遍历链接,一个每次走一步,另一个每次走两步,一个遍历完 ...
- 206. Reverse Linked List - LeetCode
Question 206. Reverse Linked List Solution 题目大意:对一个链表进行反转 思路: Java实现: public ListNode reverseList(Li ...
随机推荐
- 【NOIP模拟赛】黑红树 期望概率dp
这是一道比较水的期望概率dp但是考场想歪了.......我们可以发现奇数一定是不能掉下来的,因为若奇数掉下来那么上一次偶数一定不会好好待着,那么我们考虑,一个点掉下来一定是有h/2-1个红(黑),h/ ...
- js生成图片
var image = new Image(); var c = document.getElementById("myCanvas"); var ctx = c.getConte ...
- [hdu 4734]数位dp例题
通过这个题目更加深入了解到了数位dp在记忆化搜索的过程中就是实现了没有限制条件的n位数的状态复用. #include<bits/stdc++.h> using namespace std; ...
- wyh的物品~(二分)
链接:https://www.nowcoder.com/acm/contest/93/I来源:牛客网 题目描述 wyh学长现在手里有n个物品,这n个物品的重量和价值都告诉你,然后现在让你从中选取k个, ...
- Spring Framework框架解析(1)- 从图书馆示例来看xml文件的加载过程
引言 这个系列是我阅读Spring源码后的一个总结,会从Spring Framework框架的整体结构进行分析,不会先入为主的讲解IOC或者AOP的原理,如果读者有使用Spring的经验再好不过.鉴于 ...
- Web项目中加载Spring配置的常用方法
1.web.xml中添加配置 <web-app> <context-param> <param-name>contextConfigLoc ...
- Activiti工作流引擎核心介绍
引言 Activiti 作为一个遵从 Apache 许可的工作流和业务流程管理开源平台,其核心是基于 Java 的超快速.超稳定的 BPMN 2.0 流程引擎,强调流程服务的可嵌入性和可扩展性,同时更 ...
- 转:A Painless Q-learning Tutorial (一个 Q-learning 算法的简明教程)
demo 参见 MDP DEMO 本文是对 http://mnemstudio.org/path-finding-q-learning-tutorial.htm 的翻译,共分两部分,第一部分为中文 ...
- nginx重要配置项简要说明
1.重要配置项 以下是一个完整的nginx配置信息. #================================以下是全局配置项 #指定运行nginx的用户和用户组,默认情况下该选项关闭(关闭 ...
- c:forEach 如何输出序号
关键在于<c:forEach>的varStatus属性,具体代码如下: <table width="500" border="0" cells ...