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 ...
随机推荐
- POJ2112:Optimal Milking(Floyd+二分图多重匹配+二分)
Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 20262 Accepted: 7230 ...
- codeforces 1015E1&&E2
E1. Stars Drawing (Easy Edition) time limit per test 3 seconds memory limit per test 256 megabytes i ...
- spring 中的@Import注解和@ImportResource注解
概述:@Import注解是引入带有@Configuration的java类. @ImportResource是引入spring配置文件.xml 案例的核心代码如下: package com.timo. ...
- QT QLayout 清空
QLayout* pLayout = (QLayout*)ui->frame->layout(); )) { QWidget* pWidget = child->widget(); ...
- spring+jersey+c3p0构建restful webservice(数据源采用c3p0)
项目下载地址: http://files.cnblogs.com/files/walk-the-Line/payment.zip
- 转:强化学习(Reinforcement Learning)
机器学习算法大致可以分为三种: 1. 监督学习(如回归,分类) 2. 非监督学习(如聚类,降维) 3. 增强学习 什么是增强学习呢? 增强学习(reinforcementlearning, RL)又叫 ...
- MySQL中大于等于小于等于的写法
由于在mybatis框架的xml中<= , >=解析会出现问题,编译报错,所以需要转译 第一种写法: 原符号 < <= > >= & ' " 替换 ...
- Flex UI刷新后保持DataGrid中的ScrollBar的位置不变
这是之前我发的一个贴子问题描述:http://q.cnblogs.com/q/53469/
- python的tuple()
描述 Python 元组 tuple() 函数将列表转换为元组. 语法 tuple()方法语法: tuple( seq ) 参数 seq -- 要转换为元组的序列. 返回值 返回元组. 实例 以下实例 ...
- codefoeces 671 problem D
D. Roads in Yusland standard output Mayor of Yusland just won the lottery and decided to spent money ...