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 ...
随机推荐
- 13 个免费的 PNG 图像的优化和压缩工具
图像格式有许多种不同类型,在互联网上最常见的有JPEG.GIF.BMP.TIFF和PNG.每一种图像格式都有它自己的用途,比如GIF是用于动画的,JPEG是用于高清图片的,这种图片在保存或者调整大小后 ...
- log4j输出日志到文件
输出端Appender Appender用来指定日志信息输出到哪个地方,可以同时指定多个输出目的地.Log4j允许将信息输出到许多不同的输出设备中,一个log信息输出目的地就叫做一个Appender. ...
- CSS创造三角形的原理
其实就是利用了div各方向border的接驳点产生的斜线的特点,知道原理后就不觉得有多不可思议了.. .triangle_up { height: 0px; width: 0px; border-bo ...
- javascript版快速排序和冒泡排序
var sort = (function () { //快速排序 var quickSort = { partition: function (array, low, high) { if (low ...
- Powerdesigner SqlServer转Oracle(转)
参考文章,原文地址:http://blog.csdn.net/cicada688/article/details/7802881 问题1:sqlserver数据库直接转oracle.字段类型由sql ...
- ionic 通过PouchDB + SQLite来实现app的本地存储(Local Storage)
首先声明,本教程参考国外网站(http://gonehybrid.com/how-to-use-pouchdb-sqlite-for-local-storage-in-your-ionic-app/) ...
- 解决因为使用了官方xbean-2.4.0.jar 的库造成的性能问题
最近我们游戏经常收到玩家投诉卡进度条的问题.而且后台显示执行队列和CPU使用率异常高 根据调用的JDB分析出 使用xbean 时候会调用以下代码 在设置xmlobject 时候会有一个 GlobalL ...
- C#基础总结
1.执行.NET应用程序时经历的几个步骤 用C#编写应用程序代码 把应用程序代码编译为中间语言代码(MSIL),存储在程序集中 使用JIT编译器将MSIL编译为本机代码 在托管的公共语言运行库(CLR ...
- centos mongodb cluster install 完全版
分享一则用yum安装的mongodb初始化脚本. install_mongodb.sh会安装mongodb所需的基本环境. 配置副本集的时候会需要辅助文件hosts.conf. 说明:该示例为一主一丛 ...
- [转载]SQL Server如何保证可空字段中非空值唯一
原文地址:http://www.cnblogs.com/caspnet/archive/2011/02/23/1962638.html 解决问题是:在一个表里面,有一个允许为空的字段,空是可以重复的, ...