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:
1->2->3 Output:
1->2->4
ListNode* plusOne(ListNode* head) {
ListNode *l1 = reverse(head);
ListNode* cur = l1, *nh = NULL;
int c = ;
while (cur != NULL) {
cur->val += c;
c = cur->val / ;
cur->val %= ;
cur = cur->next;
}
l1 = reverse(l1); if (c != ) {
nh = new ListNode(c);
nh->next = l1;
l1 = nh;
}
return l1;
} ListNode* reverse(ListNode* head) {
ListNode* prev = NULL;
ListNode* cur = head;
ListNode* nxt = NULL;
while (cur != NULL) {
nxt = cur->next;
cur->next = prev;
prev = cur;
cur = nxt;
}
return prev;
}
public class Solution {
// two pointer
public ListNode plusOne(ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode i = dummy;
ListNode j = dummy; while (j.next != null) {
j = j.next;
if (j.val != 9) {
i = j;
}
}
// i = index of last non-9 digit i.val++;
i = i.next;
while (i != null) {
i.val = 0;
i = i.next;
} if (dummy.val == 0) return dummy.next;
return dummy;
}
}

369. Plus One Linked List的更多相关文章

  1. LeetCode 369. Plus One Linked List

    原题链接在这里:https://leetcode.com/problems/plus-one-linked-list/ 题目: Given a non-negative number represen ...

  2. [LeetCode] 369. Plus One Linked List 链表加一运算

    Given a non-negative number represented as a singly linked list of digits, plus one to the number. T ...

  3. <LinkedList> 369 (高)143 (第二遍)142 148

    369. Plus One Linked List 1.第1次while: 从前往后找到第一个不是9的位,记录. 2.第2次while: 此位+1,后面的所有值设为0(因为后面的位都是9). 返回时注 ...

  4. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  5. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  6. 66. Plus One 数组加1

    [抄题]: Given a non-negative integer represented as a non-empty array of digits, plus one to the integ ...

  7. LeetCode分类-前400题

    1. Array 基础 27 Remove Element 26 Remove Duplicates from Sorted Array 80 Remove Duplicates from Sorte ...

  8. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  9. Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

    All LeetCode Questions List 题目汇总 Sorted by frequency of problems that appear in real interviews. Las ...

随机推荐

  1. ireport5.6+jasperreport6.3开发(二)--web开发的配置

    ireport5.6只能编译出*.jasper的报表包,最终报表需要被输出为一个doc html pdf excel等文件,这时就需要jasperreport6.5的库进行配合了. jasperrep ...

  2. linux笔记:shell编程-文本处理命令

    cut(字段提取命令,也叫列提取命令): printf(格式化输出命令): awk(awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理): sed(sed是一个很好 ...

  3. LabVIEW之生产者/消费者模式--队列操作 彭会锋

    LabVIEW之生产者/消费者模式--队列操作 彭会锋 本文章主要是对学习LabVIEW之生产者/消费者模式的学习笔记,其中涉及到同步控制技术-队列.事件.状态机.生产者-消费者模式,这几种技术在在本 ...

  4. angularjs自定义指令

    my-directive为指令名称,thisdata为绑定的数据 <span ng-repeat="act in move.casts" style="positi ...

  5. archlinux 安装手记

    Wiki常用软件 https://wiki.archlinux.org/index.php/Common_Applications -> 移动硬盘等的自动挂载 pacman -S gvfs-af ...

  6. Sql Server 删除所有表(转)

    http://www.cnblogs.com/jys509/p/3589468.html  首先必须要清空所有表的外键 DECLARE c1 cursor for select 'alter tabl ...

  7. 超级文件搜索器(SuperSearch)

    写了个工具,各个分享渠道上传太难了,在这里分享给大家吧! 中文名称:超级文件搜索器 英文名称:  SuperSearch 适用对象:硬盘里文件达到数万至数千万级的用户 运行环境:WindowXP,Wi ...

  8. Could not load file or assembly or one of its dependencies. 试图加载格式不正确的程序。

    当我把编译好的程序托管到IIS下后,访问时出现了以下问题,服务器环境是IIS 7,操作系统 Windows server 2008 r2 64位. 出现上述问题的原因是,所加载的程序集中有32位的,也 ...

  9. eclipse导入外部jar包

    首先在项目下创建一个文件夹,保存我们的jar包. 在项目名上右击,依次点击[New]-->[Floder],打开新建文件夹窗口输入文件夹名称[lib],点击[ok].我们通常在lib文件夹中存放 ...

  10. JVM 虚拟机 内存说明

    Java虚拟机所管理的内存图: 1程序计数器: 该模块作用是记录线程执行字节码的位置,记录程序执行状态,以便该线程下次获得CPU时继续执行.程序里的每个线程程序计数器相互独立,互不影响,该内存区也称“ ...