369. Plus One Linked List
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的更多相关文章
- LeetCode 369. Plus One Linked List
原题链接在这里:https://leetcode.com/problems/plus-one-linked-list/ 题目: Given a non-negative number represen ...
- [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 ...
- <LinkedList> 369 (高)143 (第二遍)142 148
369. Plus One Linked List 1.第1次while: 从前往后找到第一个不是9的位,记录. 2.第2次while: 此位+1,后面的所有值设为0(因为后面的位都是9). 返回时注 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- 66. Plus One 数组加1
[抄题]: Given a non-negative integer represented as a non-empty array of digits, plus one to the integ ...
- LeetCode分类-前400题
1. Array 基础 27 Remove Element 26 Remove Duplicates from Sorted Array 80 Remove Duplicates from Sorte ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- 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 ...
随机推荐
- Struts2标签之Checkbox
<s:checkbox name="permisskey" label="帅哥" value="true" fieldValue=&q ...
- laravel 安装完成后安装 vendor 目录
composer install 就是将composer.js 中的配置下载安装 生成vendor目录
- TortoiseGit编辑全局变量支持https
在windows,右键,进入tortoisegit的设置窗口,在左边树形菜单选Git,然后店"编辑全局.gid/config"按钮 输入以下文字 [http] sslVerify ...
- Java双循环break的用法
break只跳出当前循环,也就是内循环,如果想跳出外循环有两种办法:1:for(int i = 0;i<9;i++){ //用两个breakfor(int j = 0;j<8;j++){b ...
- css3动态边框
<ul class="partnersLogo"> <li> <a href="http://www.chinapnr.com/" ...
- n进制转化成十进制
一个字节是8位二进制. 我的天啊.我发现char特别厉害: char类型能够输入整形常量.不能超过-128~127. 只能是整数类型,且范围一般不能超出-128~127.超出会溢出的.
- 03-方法ppt动手动脑问题及课后实验性问题总结
一.如何不使用static来进行调用函数? 1.自己定义的不需要都是static,但是要在主函数中调用就需要static了,因为main是静态 的,在类加载时就加载了.如果想用又不加可以吧自己写的方法 ...
- C#线程通信与异步委托
线程的通知机制 AutoResetEvent是线程实现通知操作的重要方法.通常,AutoResetEvent用于通知正在等待线程已发生事件,允许线程通过发信号互相通信. AutoResetEvent时 ...
- Luogu 魔法学院杯-第二弹(萌新的第一法blog)
虽然有点久远 还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题 沉迷游戏,伤感情 #include <queue> ...
- Spring MVC常用注解
cp by http://www.cnblogs.com/leskang/p/5445698.html 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由Di ...