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 ...
随机推荐
- linux command
ubuntu start network:sudo service network-manager start
- ireport5.6+jasperreport6.3开发(五)--以javabean为基准的报表开发(action关联)
这里的是定方法主要参照sturts2-jasperreport-plugin的完成方法(其实就是抄的) PDF的样子是这样的两页的pdf 然后action的配置是这样的(不要在意格式) @Parent ...
- Nodejs express中创建ejs项目,解决express下默认创建jade,无法创建ejs问题
最近在看<Node.js开发指南>,看到使用nodejs进行web开发的时候,准备创建ejs项目遇到问题了, 书上命令为: express -t ejs microblog 可是执行后,仍 ...
- Takeown--夺取文件or文件夹所有权
强制将当前目录下的所有文件及文件夹.子文件夹下的所有者更改为管理员组(administrators)命令:takeown /f * /a /r /d y 将所有d:\documents目录下的文件.子 ...
- Mvc 提交表单的4种方法全程详解
一,MVC HtmlHelper方法 Html.BeginForm(actionName,controllerName,method,htmlAttributes){} BeginRouteForm ...
- angularJs|es6|reactJs|vueJs相关技术(请访问https://expendo.github.io/)
技术博客地址:https://expendo.github.io/
- js中转移符
"<a href='javascript:;' onclick='javascript:changeChannelRuleStatus(\"" + options. ...
- NoSQL生态系统——事务机制,行锁,LSM,缓存多次写操作,RWN
13.2.4 事务机制 NoSQL系统通常注重性能和扩展性,而非事务机制. 传统的SQL数据库的事务通常都是支持ACID的强事务机制.要保证数据的一致性,通常多个事务是不可能交叉执行的,这样就导致了可 ...
- 关于KINECT2 和ROS接口安装的问题
具体安装过程见此博客 http://www.itdadao.com/articles/c15a450477p0.html 感谢博主. 注意,在我自己的电脑上,最后测试的两条代码执行不了,即:1)ros ...
- C# 图片的裁剪,两个图片合成一个图片
图片的裁剪,两个图片合成一个图片(这是从网上摘的) /// <summary> /// 图片裁剪,生成新图,保存在同一目录下,名字加_new,格式1.png 新图1_ne ...