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. centos6.5安装lnmp环境

    1.安装nignx的源,默认cenots6没有的. rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-cent ...

  2. Leetcode--Merge Two Sorted Lists

    static ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { ListNode *temp = ); ListNode *head = te ...

  3. java的前台与后台

    技术上:前台是指web展示,webservice接口等输入输出接口,后台是指支持这些接口的程序. 例如读写数据库,读写文件,业务逻辑处理. 业务上来讲:前台是提供给最终用户使用的界面,后台是指管理使用 ...

  4. sessionStorage & localStorage & cookie

    sessionStorage & localStorage & cookie 概念 html5中的Web Storage包括了两种存储方式:sessionStorage和localSt ...

  5. H5如何实现一行三列布局

    <!DOCTYPE html><html lang="en"><head>         <meta charset="UTF ...

  6. Socket通信 简单实现私聊、群聊(dos命令下)

    很久以前的一个Demo,这里服务器只做转发功能,根据ID地址和端口号来标识身份,群聊和私聊只是简单实现, 服务器代码如下: import java.util.*; import java.io.*; ...

  7. whereis 和which

    这两个命令用的好,可以很快找出文件的路径 [root@oc3408554812 zip-3.0]# which passwd/usr/bin/passwd[root@oc3408554812 zip- ...

  8. 数据加密标准——DES

    DES算法和DESede算法统称DES系列算法.DES算法是对称加密算法领域中的典型算法,为后续对称加密算法的发展奠定了坚实的基础.但是DES算法密钥偏短,仅有56位,迭代次数偏少,受到诸如查分密码分 ...

  9. Java 设计模式之代理模式

    1.  定义:为其它对象提供一种代理以控制对这个对象的访问.在某些情况下,一个对象不适合或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用. 2.  类图:代理对象和被代理 ...

  10. 请定义一个宏,比较两个数a、b的大小,不能使用大于、小于、if语句

    提供一种算法: #define max(a, b) ((((a)-(b)) + fabs(a-b))?(a):(b))