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;
        ;
        while (cur != NULL) {
            cur->val += c;
            c = cur->val / ;
            cur->val %= ;
            cur = cur->next;
        }
        l1 = reverse(l1);

        ) {
            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 All in One 题目讲解汇总(持续更新中...)

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

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

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

  4. 66. Plus One 数组加1

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

  5. LeetCode分类-前400题

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

  6. [LeetCode] Linked List Random Node 链表随机节点

    Given a singly linked list, return a random node's value from the linked list. Each node must have t ...

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

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

  8. [LeetCode] Odd Even Linked List 奇偶链表

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  9. [LeetCode] Delete Node in a Linked List 删除链表的节点

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

随机推荐

  1. ecshop 后台 审核功能

    有三个关键文件 html文件<img src="images/{if $vo.is_check}yes{else}no{/if}.gif" onclick="lis ...

  2. js对已经对象类型进行类型辨别

    typeof() Object.prototype.toString.call(data)

  3. JavaScript Boolean 对象

    JavaScript Boolean 对象 Boolean 对象 Boolean 对象用于转换一个不是 Boolean 类型的值转换为 Boolean 类型值 (true 或者false). Bool ...

  4. 使用 Vagrant 打造跨平台开发环境

    Vagrant 是一款用来构建虚拟开发环境的工具,非常适合 php/python/ruby/java 这类语言开发 web 应用,“代码在我机子上运行没有问题”这种说辞将成为历史. 我们可以通过 Va ...

  5. 共享池之六:shared pool latch/ library cache latch /lock pin 简介

    latch:library cache --desc v$librarycache; latch:library cache用于保护hash bucket.library cache lock保护HA ...

  6. Android开发-API指南-&lt;action&gt;

    <action> 英文原文:http://developer.android.com/guide/topics/manifest/action-element.html 采集(更新)日期: ...

  7. 实时显示GetLastError值

    在Watch窗口中输入$err,hr 自动显示上个函数返回值

  8. C# WinForm动态调用远程Web服务

    本文转自:http://blog.csdn.net/muyangjun/article/details/7930871 1.添加服务引用 2.在弹出的添加服务引用对话框地址栏中输入WebService ...

  9. 实现apk 调用framework java JNI中方法

    首先整个实现需要有Android源码编译环境.这里我用的是froyo2.2. 1.JNI层--C++代码部分 在目录frameworks/base/core/jni 下创建android_jnidem ...

  10. vbs 读取txt是读取特定的行

    读取第三行的内容:Set fso = CreateObject("scripting.filesystemobject")path="A.TXT文件的路径"Se ...