原题链接在这里: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. jQuery和AngularJS的区别小分析

    最近一直在研究angularjs,最大的感受就是它和之前的jQuery以及基于jQuery的各种库设计理念完全不同,如果不能认识到这点而对于之前做jQuery开发的程序员,去直接学习angularjs ...

  2. 事务日志以及虚拟日志文件(VLFs)概述

    Part 1:事务日志 每个 SQL Server 数据库都具有事务日志,用于记录所有事务以及每个事务对数据库所做的修改.必须定期截断事务日志以避免它被填满.但是,一些因素可能延迟日志截断,因此监视日 ...

  3. 8-07CONTIUE 、 BREAK、RETURN

    CONTIUE: 可以让程序跳过CONTIUE关键字之后的语句,回到WHILE循环的第一行命令. BREAK:让程序跳出循环,结束WHILE的循环. BREAK: 让系统完全跳出循环,结束WHILE循 ...

  4. codeforces Round#381 div2

    第一题: 按余数分类,1,2,3分别由哪些基数组成 1->[1][2+3][3+3+3] 2->[1+1][2][3+3] 3->[1+1+1][1+2][3] #include&l ...

  5. jQuery基础知识准备

    一. 代码风格在jQuery程序中,不管是页面元素的选择.内置的功能函数,都是美元符号"$"来起始的.而这个"$"就是jQuery当中最重要且独有的对象:jQu ...

  6. 在wex5平台grid里面的gridselect下拉不能显示汉字问题

    当grid里面有gridSelect组件的时候,gridSelect里面的bind-ref是对应的数据库存入字段(int类型),bind-labelRef是对应的计算字段(视图里面的),而option ...

  7. C#基础:委托

    委托是C#中最为常见的内容.与类.枚举.结构.接口一样,委托也是一种类型.类是对象的抽象,而委托则可以看成是函数的抽象.一个委托代表了具有相同参数列表和返回值的所有函数.比如: delegate in ...

  8. Java-Android【1】-控制手机震动

    一.配置震动授权 1.在AndroidManifest.xml文件中添加<manifest></manifest>中添加一行 <uses-permission andro ...

  9. PHPExcel——读取excel

    在网上找了excel读取的一些资料,个人觉得PHPExcel这还是挺好用的,相对比较全的工具. 主要功能是读取上传的excel文件,插入或更新到数据库. iconv("gbk",& ...

  10. jQuery插件 -- 动态事件绑定插件jquery.livequery.js

    http://blog.csdn.net/zzq58157383/article/details/7721974 动态事件绑定插件livequery, 可以利用它给相应的DOM元素注册事件或者触发回调 ...