|
3 | 3 | import com.fishercoder.common.classes.ListNode;
|
4 | 4 |
|
5 | 5 | /**
|
| 6 | + * 369. Plus One Linked List |
| 7 | + * |
6 | 8 | * Given a non-negative integer represented as non-empty a singly linked list of digits, plus one to the integer.
|
7 | 9 |
|
8 | 10 | You may assume the integer do not contain any leading zero, except the number 0 itself.
|
|
18 | 20 | */
|
19 | 21 | public class _369 {
|
20 | 22 |
|
21 |
| - public ListNode plusOne(ListNode head) { |
22 |
| - //get the length of the list and take out the value of each node and store them into an array |
23 |
| - ListNode temp = head; |
24 |
| - int len = 0; |
25 |
| - while (temp != null) { |
26 |
| - len++; |
27 |
| - temp = temp.next; |
28 |
| - } |
29 |
| - |
30 |
| - int[] nums = new int[len]; |
31 |
| - temp = head; |
32 |
| - int j = 0; |
33 |
| - while (temp != null) { |
34 |
| - nums[j++] = temp.val; |
35 |
| - temp = temp.next; |
36 |
| - } |
| 23 | + public static class Solution1 { |
| 24 | + public ListNode plusOne(ListNode head) { |
| 25 | + //get the length of the list and take out the value of each node and store them into an array |
| 26 | + ListNode temp = head; |
| 27 | + int len = 0; |
| 28 | + while (temp != null) { |
| 29 | + len++; |
| 30 | + temp = temp.next; |
| 31 | + } |
37 | 32 |
|
38 |
| - //plus one into this array: nums |
39 |
| - for (int i = len - 1; i >= 0; i--) { |
40 |
| - if (nums[i] != 9) { |
41 |
| - nums[i]++; |
42 |
| - break; |
43 |
| - } else { |
44 |
| - nums[i] = 0; |
| 33 | + int[] nums = new int[len]; |
| 34 | + temp = head; |
| 35 | + int j = 0; |
| 36 | + while (temp != null) { |
| 37 | + nums[j++] = temp.val; |
| 38 | + temp = temp.next; |
45 | 39 | }
|
46 |
| - } |
47 | 40 |
|
48 |
| - //still assuming the first value in the list should not be zero as it's representing a valid number, although it's in a list |
49 |
| - ListNode pre = new ListNode(-1); |
50 |
| - if (nums[0] == 0) { |
51 |
| - //in this case, let's just construct a new linked list and return: only first node value is 1, all the rest is 0 |
52 |
| - ListNode newHead = new ListNode(1); |
53 |
| - ListNode result = newHead; |
54 |
| - int count = 0; |
55 |
| - while (count++ < len) { |
56 |
| - newHead.next = new ListNode(0); |
57 |
| - newHead = newHead.next; |
| 41 | + //plus one into this array: nums |
| 42 | + for (int i = len - 1; i >= 0; i--) { |
| 43 | + if (nums[i] != 9) { |
| 44 | + nums[i]++; |
| 45 | + break; |
| 46 | + } else { |
| 47 | + nums[i] = 0; |
| 48 | + } |
58 | 49 | }
|
59 |
| - return result; |
60 |
| - } else { |
61 |
| - pre.next = head; |
62 |
| - for (int i = 0; i < len; i++) { |
63 |
| - head.val = nums[i]; |
64 |
| - head = head.next; |
| 50 | + |
| 51 | + //still assuming the first value in the list should not be zero as it's representing a valid number, although it's in a list |
| 52 | + ListNode pre = new ListNode(-1); |
| 53 | + if (nums[0] == 0) { |
| 54 | + //in this case, let's just construct a new linked list and return: only first node value is 1, all the rest is 0 |
| 55 | + ListNode newHead = new ListNode(1); |
| 56 | + ListNode result = newHead; |
| 57 | + int count = 0; |
| 58 | + while (count++ < len) { |
| 59 | + newHead.next = new ListNode(0); |
| 60 | + newHead = newHead.next; |
| 61 | + } |
| 62 | + return result; |
| 63 | + } else { |
| 64 | + pre.next = head; |
| 65 | + for (int i = 0; i < len; i++) { |
| 66 | + head.val = nums[i]; |
| 67 | + head = head.next; |
| 68 | + } |
| 69 | + return pre.next; |
65 | 70 | }
|
66 |
| - return pre.next; |
67 | 71 | }
|
68 |
| - |
69 | 72 | }
|
70 | 73 |
|
71 | 74 | }
|
0 commit comments