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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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: ...

  4. 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 ...

  5. Linked List - leetcode

    138. Copy List with Random Pointer //不从head走 前面加一个dummy node 从dummy走先连head 只需记录当前节点 //这样就不需要考虑是先new ...

  6. 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 ...

  7. 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 ...

  8. 876. Middle of the Linked List - LeetCode

    Question 876. Middle of the Linked List Solution 题目大意:求链表的中间节点 思路:构造两个节点,遍历链接,一个每次走一步,另一个每次走两步,一个遍历完 ...

  9. 206. Reverse Linked List - LeetCode

    Question 206. Reverse Linked List Solution 题目大意:对一个链表进行反转 思路: Java实现: public ListNode reverseList(Li ...

随机推荐

  1. hadoop基础----hadoop实战(七)-----hadoop管理工具---使用Cloudera Manager安装Hadoop---Cloudera Manager和CDH5.8离线安装

    hadoop基础----hadoop实战(六)-----hadoop管理工具---Cloudera Manager---CDH介绍 简介 我们在上篇文章中已经了解了CDH,为了后续的学习,我们本章就来 ...

  2. 【CF edu 30 C. Strange Game On Matrix】

    time limit per test 1 second memory limit per test  256 megabytes input standard input output standa ...

  3. 【BZOJ 1407】[Noi2002]Savage ExGCD

    我bitset+二分未遂后就来用ExGCD了,然而这道题的时间复杂度还真是玄学...... 我们枚举m然后对每一对用ExGCD判解,我们只要满足在最小的一方死亡之前无解就可以了,对于怎么用,就是ax+ ...

  4. 收藏一个漂亮的Flash焦点图切换

    网上闲逛的时候发现一个Flash焦点图效果,跟喜欢,然后就下载回来,收集在这里,以便以后方便取用.这个Flash使用方法也是相当简单的,如果你喜欢,也可以从这里查看源代码下载. Flash 焦点图效果 ...

  5. iOS 快速框架搭建项目

    项目目录 Pod第三方 gitHub地址 https://github.com/henusjj/Basic-project-framework

  6. 着色方案(bzoj 1079)

    Description 有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块.所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n.相邻两个木 ...

  7. 使用FindBugs-IDEA插件找到代码中潜在的问题

    另一篇使用文档,参照:https://www.cnblogs.com/huaxingtianxia/p/6703315.html 我们通常都会在APP上线之后,发现各种错误,尤其是空指针异常,这些错误 ...

  8. CVE-2017-5521: Bypassing Authentication on NETGEAR Routers(Netgear认证绕过漏洞)

    SpiderLabs昨天发布的漏洞, 用户访问路由器的web控制界面尝试身份验证,然后又取消身份验证,用户就会被重定向到一个页面暴露密码恢复的token.然后通过passwordrecovered.c ...

  9. iOS推送原理和证书生成简介

    1. 推送流程: Provider: 我们自己的后台服务器: APNS: 苹果的消息推送服务器 (1) 当Provider有消息要推送给手机的时候,先将消息和deviceToken等字段发送到APNS ...

  10. xcode/Interface Build(IB)/iPhone模拟器/mac/组合键常用的命令集

    1.Xcode常用快捷键: win+N:新建文件 win+shift+N:新建工程 win+O:打开工程或文件 win+S:保存 win+shift+S:另存为 win+Z:撤销一步 win+W:关闭 ...