原题链接在这里:https://leetcode.com/problems/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

题解:

Method 1:

末位加一,若是有进位,就要在 Linked List 的前一个 Node 做改动,自然而然想到先Reverse Linked List. 从头开始做比较方便. 加完再reverse回来.

Time Complexity: O(n). reverse 用O(n), reverse 两次 加上 iterate 一次.

Space: O(1).

AC Java:

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode plusOne(ListNode head) {
if(head == null){
return head;
}
ListNode tail = reverse(head);
ListNode cur = tail;
ListNode pre = cur;
int carry = 1; while(cur != null){
pre = cur;
int sum = cur.val + carry;
cur.val = sum%10;
carry = sum/10;
if(carry == 0){
break;
}
cur = cur.next;
}
if(carry == 1){
pre.next = new ListNode(1);
}
return reverse(tail);
} private ListNode reverse(ListNode head){
if(head == null || head.next == null){
return head;
}
ListNode tail = head;
ListNode cur = head;
ListNode pre;
ListNode temp; while(tail.next != null){
pre = cur;
cur = tail.next;
temp = cur.next;
cur.next = pre;
tail.next = temp;
}
return cur;
}
}

Method 2:

利用递归,因为递归的终止条件就是到了末尾节点,每层向上返回一个carry数值.

Time Complexity: O(n), 最多每个节点iterate两遍.

Space: O(n), recursion用了n层stack.

AC Java:

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode plusOne(ListNode head) {
if(head == null){
return head;
}
int carry = dfs(head);
if(carry == 1){
ListNode dummy = new ListNode(1);
dummy.next = head;
return dummy;
}
return head;
} private int dfs(ListNode head){
if(head == null){
return 1;
}
int carry = dfs(head.next);
int sum = head.val + carry;
head.val = sum%10;
return sum/10;
}
}

Method 3:

和Method 2原题相同,用stack代替recursion.

Time Complexity: O(n).

Space: O(n). Stack 大小.

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode plusOne(ListNode head) {
if(head == null){
return head;
}
Stack<ListNode> stk = new Stack<ListNode>();
ListNode cur = head;
while(cur != null){
stk.push(cur);
cur = cur.next;
}
int carry = 1;
while(!stk.isEmpty() && carry == 1){
ListNode top = stk.pop();
int sum = top.val + carry;
top.val = sum%10;
carry = sum/10;
}
if(carry == 1){
ListNode dummy = new ListNode(1);
dummy.next = head;
return dummy;
}
return head;
}
}

Method 4:

从右向左寻找第一个不是9的节点,找到后在该节点加一,  若是他后面还有节点, 说明后面的节点都是9, 所以都要变成0.

Time Complexity: O(n).

Space: O(1).

AC Java:

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode plusOne(ListNode head) {
if(head == null){
return head;
} ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode cur = dummy;
ListNode notNine = dummy;;
while(cur != null){
if(cur.val != 9){
notNine = cur;
}
cur = cur.next;
} notNine.val += 1;
cur = notNine.next;
while(cur != null){
cur.val = 0;
cur = cur.next;
}
return notNine == dummy ? dummy : head;
}
}

LeetCode Plus One Linked List的更多相关文章

  1. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  2. [LeetCode] 92. Reverse Linked List II_Medium tag: Linked List

    Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...

  3. [LeetCode] 92. Reverse Linked List II 反向链表II

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

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

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

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

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

  6. 【LeetCode OJ】Linked List Cycle

    Problem link: http://oj.leetcode.com/problems/linked-list-cycle/ We set two pointers: the faster poi ...

  7. [LeetCode] 141&142 Linked List Cycle I & II

    Problem: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without ...

  8. 【一天一道LeetCode】#141. Linked List Cycle

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. C++版 - 剑指offer 面试题16:反转链表(Leetcode 206: Reverse Linked List) 题解

    面试题16:反转链表 提交网址: http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId= ...

随机推荐

  1. UC浏览器 分享到朋友圈和微信好友 分类: JavaScript 2015-04-28 14:45 615人阅读 评论(1) 收藏

    用手机UC浏览器访问新浪微博,会注意到有这样的两个分享按钮: 在手机端浏览器里,点击分享按钮,就可以启动微信客户端并分享到微信.研究了下其源代码,存在这样的一个js:http://mjs.sinaim ...

  2. 决策树的python实现

    决策树 算法优缺点: 优点:计算复杂度不高,输出结果易于理解,对中间值缺失不敏感,可以处理不相关的特征数据 缺点:可能会产生过度匹配的问题 适用数据类型:数值型和标称型 算法思想: 1.决策树构造的整 ...

  3. CozyRSS开发记录5-订阅列表栏里的项

    CozyRSS开发记录5-订阅列表栏里的项 1.订阅列表栏里的项的原型图 这里列表项依然参考傲游的RSS阅读器,以后可能会微调. 2.使用ControlTemplate来定制ListBoxItem 给 ...

  4. Go语言 模板的使用(二)

    判断 {{if .Attended}} 如果Attended是true的话,这句是第二行 {{else}} 如果Attended是false的话,这句是第二行 {{end}} 循环 我们可以使用ran ...

  5. IE、FF、Chrome浏览器中的JS差异介绍

     FF.Chrome:没有window.event对象 FF.Chrome:没有window.event对象,只有event对象,IE里只支持window.event,而其他主流浏览器两者都支持,所以 ...

  6. 虚拟主机Dede程序安装

    1. 在网上下载最新版本DedeCMS 2. 将Dede程序通过FTP上传到主机上的htdocs目录. 3. 在主机控制台将程序解压到根目录. 4. 将解压后的 upload 目录下的文件全部移动到 ...

  7. APP性能测试之卡顿比(FPS)

    fps概念: FPS是图像领域中的定义,是指画面每秒传输帧数,通俗来讲就是指动画或视频的画面数.FPS是测量用于保存.显示动态视频的信息数量.每秒钟帧数愈多,所显示的动作就会愈流畅. 卡顿人体感觉标准 ...

  8. DEV 等待窗口

    DevExpress.Utils.WaitDialogForm dlg = , )); System.Threading.Thread.Sleep(); dlg.Close();

  9. xp系统重绘边框线不显示(首次加载没有触发paint事件)

    同样是,重绘边框事件,win7系统显示正常,而xp系统却不显示,这是什么原因造成的呢? 于是,小编开始百度,不停的查找原因,通过一番查找,小编也意外的收获了一些内容: 例如:窗口的拖动,放大,缩小,等 ...

  10. 推流和拉流的概念以及RTMP和HLS协议

    推流为将直播内容推送至服务器的过程:拉流为服务器已有直播内容,用指定地址进行拉取的过程. rtmp rtmp是Real Time Messaging Protocol(实时消息传输协议)的首字母缩写. ...