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
好吧看了半天才明白题是什么意思。
给一个连表代表一串数字,每一个节点只有一个数字,全部组成一个数值。
对数的尽头+1,然后带进位的,给出最后的结果。
answer one
Method 1:
末位加一,若是有进位,就要在 Linked List 的前一个 Node 做改动,自然而然想到先Reverse Linked List. 从头开始做比较方便. 加完再reverse回来.
Time Complexity: O(n). reverse 用O(n), reverse 两次 加上 iterate 一次.
Space: O(1).
/**
* 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;
}
}
answer two
Method 2:
利用递归,因为递归的终止条件就是到了末尾节点,每层向上返回一个carry数值.
Time Complexity: O(n), 最多每个节点iterate两遍.
Space: O(n), recursion用了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;
}
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;
}
}
answer three
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;
}
}
answer four
Method 4:
从右向左寻找第一个不是9的节点,找到后在该节点加一, 若是他后面还有节点, 说明后面的节点都是9, 所以都要变成0.
Time Complexity: O(n).
Space: O(1).
/**
* 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;
}
}