Skip to content

Commit 08de32a

Browse files
committed
refactor code: extract ListNode and utils
1 parent 24034f7 commit 08de32a

File tree

6 files changed

+100
-170
lines changed

6 files changed

+100
-170
lines changed

src/main/java/com/diguage/algorithm/leetcode/AddTwoNumbers.java

+5-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.diguage.algorithm.leetcode;
22

3+
import com.diguage.algorithm.util.ListNode;
4+
35
import java.util.Objects;
46

7+
import static com.diguage.algorithm.util.ListNodeUtils.printListNode;
8+
59
/**
610
* = 2. Add Two Numbers
711
*
@@ -92,7 +96,7 @@ public static void main(String[] args) {
9296
ListNode list1 = result.convertNumberToList(1);
9397
ListNode list2 = result.convertNumberToList(99);
9498
ListNode sum = result.addTwoNumbers(list1, list2);
95-
iterate(sum);
99+
printListNode(sum);
96100
}
97101

98102

@@ -113,22 +117,4 @@ public ListNode convertNumberToList(int number) {
113117
}
114118
return result;
115119
}
116-
117-
public static void iterate(ListNode header) {
118-
System.out.println("\n");
119-
while (Objects.nonNull(header)) {
120-
System.out.print(header.val + "→");
121-
header = header.next;
122-
}
123-
System.out.println("\n");
124-
}
125-
126-
public static class ListNode {
127-
int val;
128-
ListNode next;
129-
130-
ListNode(int x) {
131-
val = x;
132-
}
133-
}
134120
}

src/main/java/com/diguage/algorithm/leetcode/MergeKSortedLists.java

+4-50
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import java.util.*;
44

5+
import com.diguage.algorithm.util.ListNode;
6+
7+
import static com.diguage.algorithm.util.ListNodeUtils.*;
8+
59
/**
610
* = 23. Merge k Sorted Lists
711
*
@@ -67,55 +71,5 @@ public static void main(String[] args) {
6771
ListNode r1 = solution.mergeKLists(lists);
6872
System.out.println(isOrder(r1));
6973
}
70-
71-
private static class ListNode {
72-
int val;
73-
ListNode next;
74-
75-
ListNode(int x) {
76-
val = x;
77-
}
78-
}
79-
80-
private static boolean isOrder(ListNode head) {
81-
if (Objects.isNull(head)) {
82-
return true;
83-
}
84-
System.out.println("\n--------");
85-
int last = head.val;
86-
ListNode pointer = head;
87-
while (Objects.nonNull(pointer)) {
88-
int current = pointer.val;
89-
System.out.print(current + ", ");
90-
if (last > current) {
91-
return false;
92-
}
93-
last = current;
94-
pointer = pointer.next;
95-
}
96-
System.out.println("\n--------");
97-
return true;
98-
}
99-
100-
private static ListNode generate(List<Integer> num) {
101-
if (Objects.isNull(num) || num.size() == 0) {
102-
return null;
103-
}
104-
ListNode result = null;
105-
ListNode tail = null;
106-
107-
for (int i = 0; i < num.size(); i++) {
108-
int no = num.get(i);
109-
ListNode node = new ListNode(no);
110-
if (Objects.isNull(result)) {
111-
result = node;
112-
tail = node;
113-
} else {
114-
tail.next = node;
115-
tail = node;
116-
}
117-
}
118-
return result;
119-
}
12074
}
12175

src/main/java/com/diguage/algorithm/leetcode/MergeTwoSortedLists.java

+5-51
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.diguage.algorithm.leetcode;
22

3+
import com.diguage.algorithm.util.ListNode;
4+
35
import java.util.Arrays;
4-
import java.util.List;
56
import java.util.Objects;
67

8+
import static com.diguage.algorithm.util.ListNodeUtils.generate;
9+
import static com.diguage.algorithm.util.ListNodeUtils.isOrder;
10+
711
/**
812
* = 21. Merge Two Sorted Lists
913
*
@@ -76,54 +80,4 @@ public static void main(String[] args) {
7680
ListNode r1 = solution.mergeTwoLists(l1, l2);
7781
System.out.println(isOrder(r1));
7882
}
79-
80-
private static class ListNode {
81-
int val;
82-
ListNode next;
83-
84-
ListNode(int x) {
85-
val = x;
86-
}
87-
}
88-
89-
private static boolean isOrder(ListNode head) {
90-
if (Objects.isNull(head)) {
91-
return true;
92-
}
93-
System.out.println("\n--------");
94-
int last = head.val;
95-
ListNode pointer = head;
96-
while (Objects.nonNull(pointer)) {
97-
int current = pointer.val;
98-
System.out.print(current + ", ");
99-
if (last > current) {
100-
return false;
101-
}
102-
last = current;
103-
pointer = pointer.next;
104-
}
105-
System.out.println("\n--------");
106-
return true;
107-
}
108-
109-
private static ListNode generate(List<Integer> num) {
110-
if (Objects.isNull(num) || num.size() == 0) {
111-
return null;
112-
}
113-
ListNode result = null;
114-
ListNode tail = null;
115-
116-
for (int i = 0; i < num.size(); i++) {
117-
int no = num.get(i);
118-
ListNode node = new ListNode(no);
119-
if (Objects.isNull(result)) {
120-
result = node;
121-
tail = node;
122-
} else {
123-
tail.next = node;
124-
tail = node;
125-
}
126-
}
127-
return result;
128-
}
12983
}

src/main/java/com/diguage/algorithm/leetcode/RemoveNthNodeFromEndOfList.java

+10-50
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.diguage.algorithm.leetcode;
22

3+
import com.diguage.algorithm.util.ListNode;
4+
35
import java.util.Arrays;
4-
import java.util.List;
56
import java.util.Objects;
67

8+
import static com.diguage.algorithm.util.ListNodeUtils.generate;
9+
import static com.diguage.algorithm.util.ListNodeUtils.printListNode;
10+
711
/**
812
* = 19. Remove Nth Node From End of List
913
*
@@ -64,62 +68,18 @@ public static void main(String[] args) {
6468
RemoveNthNodeFromEndOfList solution = new RemoveNthNodeFromEndOfList();
6569

6670
ListNode r5 = solution.removeNthFromEnd(generate(Arrays.asList(1, 2)), 1);
67-
print(r5);
71+
printListNode(r5);
6872

6973
ListNode r4 = solution.removeNthFromEnd(generate(Arrays.asList(1)), 1);
70-
print(r4);
74+
printListNode(r4);
7175

7276
ListNode r1 = solution.removeNthFromEnd(generate(Arrays.asList(1, 2, 3, 4, 5)), 2);
73-
print(r1);
77+
printListNode(r1);
7478

7579
ListNode r2 = solution.removeNthFromEnd(generate(Arrays.asList(1, 2, 3, 4, 5)), 6);
76-
print(r2);
80+
printListNode(r2);
7781

7882
ListNode r3 = solution.removeNthFromEnd(generate(Arrays.asList()), 2);
79-
print(r3);
80-
}
81-
82-
private static class ListNode {
83-
int val;
84-
ListNode next;
85-
86-
ListNode(int x) {
87-
val = x;
88-
}
89-
}
90-
91-
private static void print(ListNode head) {
92-
if (Objects.isNull(head)) {
93-
return;
94-
}
95-
System.out.println("\n--------");
96-
ListNode pointer = head;
97-
while (Objects.nonNull(pointer)) {
98-
int current = pointer.val;
99-
System.out.print(current + ", ");
100-
pointer = pointer.next;
101-
}
102-
System.out.println("\n--------");
103-
}
104-
105-
private static ListNode generate(List<Integer> num) {
106-
if (Objects.isNull(num) || num.size() == 0) {
107-
return null;
108-
}
109-
ListNode result = null;
110-
ListNode tail = null;
111-
112-
for (int i = 0; i < num.size(); i++) {
113-
int no = num.get(i);
114-
ListNode node = new ListNode(no);
115-
if (Objects.isNull(result)) {
116-
result = node;
117-
tail = node;
118-
} else {
119-
tail.next = node;
120-
tail = node;
121-
}
122-
}
123-
return result;
83+
printListNode(r3);
12484
}
12585
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.diguage.algorithm.util;
2+
3+
/**
4+
* @author D瓜哥, https://www.diguage.com/
5+
* @since 2019-10-29 21:07
6+
*/
7+
public class ListNode {
8+
public int val;
9+
public ListNode next;
10+
11+
public ListNode(int x) {
12+
val = x;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.diguage.algorithm.util;
2+
3+
import java.util.List;
4+
import java.util.Objects;
5+
6+
/**
7+
* ListNode 工具类
8+
*
9+
* @author D瓜哥, https://www.diguage.com/
10+
* @since 2019-10-29 21:07
11+
*/
12+
public class ListNodeUtils {
13+
public static boolean isOrder(ListNode head) {
14+
if (Objects.isNull(head)) {
15+
return true;
16+
}
17+
System.out.println("\n--------");
18+
int last = head.val;
19+
ListNode pointer = head;
20+
while (Objects.nonNull(pointer)) {
21+
int current = pointer.val;
22+
System.out.print(current + ", ");
23+
if (last > current) {
24+
return false;
25+
}
26+
last = current;
27+
pointer = pointer.next;
28+
}
29+
System.out.println("\n--------");
30+
return true;
31+
}
32+
33+
public static ListNode generate(List<Integer> num) {
34+
if (Objects.isNull(num) || num.size() == 0) {
35+
return null;
36+
}
37+
ListNode result = null;
38+
ListNode tail = null;
39+
40+
for (int i = 0; i < num.size(); i++) {
41+
int no = num.get(i);
42+
ListNode node = new ListNode(no);
43+
if (Objects.isNull(result)) {
44+
result = node;
45+
tail = node;
46+
} else {
47+
tail.next = node;
48+
tail = node;
49+
}
50+
}
51+
return result;
52+
}
53+
54+
public static void printListNode(ListNode header) {
55+
System.out.println("\n↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓");
56+
while (Objects.nonNull(header)) {
57+
System.out.print(header.val + "→");
58+
header = header.next;
59+
}
60+
System.out.println("\n↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑");
61+
}
62+
}

0 commit comments

Comments
 (0)