forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddOneToNumberInList.java
91 lines (72 loc) · 2.11 KB
/
AddOneToNumberInList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.leetcode.linkedlists;
/**
* Level: Easy
* Problem Link: https://leetcode.com/problems/plus-one-linked-list/
* Problem Description: Given a non-empty single linked list representing a number where the head is the
* most significant bit, add one to the number and return a new linked list.
*
* @author rampatra
* @since 2019-06-19
*/
public class AddOneToNumberInList {
/**
* Add {@code one} to the number represented by linked list {@code head}.
*
* @param head the starting node of the linked list
* @return the head of the linked list after adding {@code one}
*/
private static Node addOne(Node head) {
Node currOrig = reverse(head);
Node currRes = null;
Node res = null;
int sum = 1;
int carry = 0;
int rem;
while (currOrig != null) {
sum += carry + currOrig.val;
rem = sum % 10;
carry = sum / 10;
if (res == null) {
res = currRes = new Node(rem);
} else {
currRes.next = new Node(rem);
currRes = currRes.next;
}
sum = 0;
currOrig = currOrig.next;
}
if (carry != 0) {
currRes.next = new Node(carry);
}
return reverse(res);
}
private static Node reverse(Node head) {
Node prev = null;
Node curr = head;
Node next;
while (curr != null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
public static void main(String[] args) {
Node node = new Node(9);
node.next = new Node(9);
node.next.next = new Node(9);
node.print();
addOne(node).print();
System.out.println("---------");
node = new Node(1);
node.next = new Node(9);
node.next.next = new Node(9);
node.print();
addOne(node).print();
System.out.println("---------");
node = new Node(0);
node.print();
addOne(node).print();
}
}