LeetCode 369. Plus One Linked List
原题链接在这里: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 369. Plus One Linked List的更多相关文章
- [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 ...
- [LeetCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- LeetCode Intersection of Two Linked Lists
原题链接在这里:https://leetcode.com/problems/intersection-of-two-linked-lists/ 思路:1. 找到距离各自tail 相同距离的起始List ...
- 【LeetCode题解】链表Linked List
1. 链表 数组是一种顺序表,index与value之间是一种顺序映射,以\(O(1)\)的复杂度访问数据元素.但是,若要在表的中间部分插入(或删除)某一个元素时,需要将后续的数据元素进行移动,复杂度 ...
- 【一天一道Leetcode】#203.Remove Linked List Elements
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...
- 【一天一道LeetCode】#206. Reverse Linked List
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Reverse ...
- 【一天一道LeetCode】#92. Reverse Linked List II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Reverse ...
- LeetCode: Intersection of Two Linked Lists 解题报告
Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...
- LeetCode 141. 环形链表(Linked List Cycle) 19
141. 环形链表 141. Linked List Cycle 题目描述 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 ...
随机推荐
- 12.常见模块time、json模块
1.time模块 import time #python中最基本的时间模块 time.time() #时间戳 (1970年1月1日00:00:00后经过的浮点秒数) time.localtime(ti ...
- 教你在windows10环境下如何安装minepy并成功运行!
在学习使用sklearn做单机特征工程这篇文章时,发现在计算互信息时from minepy import MINE代码运行出错ModuleNotFoundError: No module named ...
- wampserver安装缺失vcruntime140.dll
wampserver安装缺失vcruntime140.dll,这是安装wamp时候经常遇到的一个问题,对于初学者来说很难解决,以前的百度经验很难解决,所以给大家一个可以用的. 方法/步骤 请先 ...
- python实现免密码登录lunx服务器
import paramikoimport oshostname='192.168.76.10'username='root'# password='123456'ssh=paramiko.SSHCl ...
- oracle 计算时间差
1.计算时间差(相隔星期,天数,小时,分钟,秒) SELECT TO_CHAR(date1,'MMDDYYYY:HH24:MI:SS') date1, TO_CHAR(date2,'MMDDYYYY: ...
- Luogu-4022 [CTSC2012]熟悉的文章
广义后缀自动机+DP 对于作文库建出广义后缀自动机,广义自动机就是在每次添加一个字符串之前把\(last=0\),然后正常添加就好了 对于每个询问串,预处理出每个位置\(i\)能向前匹配的最长长度\( ...
- ZooKeeper学习第八期---ZooKeeper伸缩性
转:http://www.cnblogs.com/sunddenly/p/4143306.html 一.ZooKeeper中Observer 1.1 ZooKeeper角色 经过前面的介绍,我想大家都 ...
- UVA 11525 Permutation (树状数组+YY)
题意:给你k个数Si,然后给你一个等式 H= ∑ Si ∗ (K − i)! (i=(1->k)且0 ≤ Si ≤ K − i). 叫你求出第H个全排列 其实这是一个康托展开:X=a[n ...
- 数据结构习题 线段树&树状数组
说明:这是去年写了一半的东西,一直存在草稿箱里,今天整理东西的时候才发现,还是把它发表出来吧.. 以下所有题目来自Lrj的<训练指南> LA 2191 单点修改,区间和 Fenwick直 ...
- Python之单例模式总结
一.单例模式 a.单例模式分为四种:文件,类,基于__new__方法实现单例模式,基于metaclass方式实现 b.类实现如下: class Sigletion(objects): import t ...