Skip to content

Commit 7ec5460

Browse files
refactor 369
1 parent b0a7b4d commit 7ec5460

File tree

1 file changed

+45
-42
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+45
-42
lines changed

src/main/java/com/fishercoder/solutions/_369.java

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.fishercoder.common.classes.ListNode;
44

55
/**
6+
* 369. Plus One Linked List
7+
*
68
* Given a non-negative integer represented as non-empty a singly linked list of digits, plus one to the integer.
79
810
You may assume the integer do not contain any leading zero, except the number 0 itself.
@@ -18,54 +20,55 @@
1820
*/
1921
public class _369 {
2022

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+
}
3732

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;
4539
}
46-
}
4740

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+
}
5849
}
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;
6570
}
66-
return pre.next;
6771
}
68-
6972
}
7073

7174
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy