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 ...
随机推荐
- Hibernate一对多、多对一关联
一对多.多对一关联:在多方加外键 示例:Group(一方)和User(多方),一个Group可以有多个User,每个User只能属于一个Group 多对一单向关联 在User(多方)中建Group ...
- UNIX 系统常用管理命令
一. 引言 UNIX系统作为一种强大的多用户分时操作系统,在越来越多的场合受到了应用,同时,对UNIX的系统管理的要求也随之越来越多,但目前的书籍对UNIX系统管理命令介绍的并不是很多.本文主要是针对 ...
- STM32F4XX高效驱动篇2 I2C
说到I2C很多用过STMF10X硬件I2C方式的工程师,都感觉有点头痛.大部分还是使用软件模拟的方式,I2C由于一般的工作频率是400,100KHz.所以在平凡读取,或所读数据量大时,使用这模拟的方式 ...
- easy_install
一 setuptools 和easy_install setuptools:setuptools 是一组由PEAK(Python Enterprise Application Kit)开发的 Pyth ...
- ==、equals与hashCode
== 首先,得说明java数据类型分为基本数据类型和引用数据类型, 基本数据类型有8种: 浮点型:float(4 byte), double(8 byte) 整型:byte(1 byte), sho ...
- Python字符串格式转换
转换类型 转换类型 说明 d, i 带符号十进制 b 无符号二进制 o 无符号八进制 u 无符号十进制 x 无符号十六进制(小写) X 无符号十六进制(大写) e 科学计数法表示的浮点数(小写) E ...
- 2.virtualenv安装和配置以及在PyCharm中如何使用虚拟环境
virtualenv优点 使不同应用开发环境相互独立 环境升级不影响其它应用,也不会影响全局的python环境 它可以防止系统中出现包管理混乱和版本的冲突 1.使用virtualenv pip ins ...
- Mybatis映射配置文件Mapper.xml详解
1.概述: MyBatis 的真正强大在于它的映射语句,也是它的魔力所在. 2.常用的属性 常用的几个属性: select元素:代表查询,类似的还有update.insert.delete id:这个 ...
- 搭建TXManager分布式事务协调者
事务分组id 缓存到redis 需要配置连接到自己的 redis地址 启动后:
- webview 最简单的demo
) { return; } view.loadUrl(url); }} <!--activity_test.xml> <?xml version="1.0" en ...