Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit992a059

Browse files
move two methods from ListNode to LinkedListUtils class
1 parent6543564 commit992a059

File tree

12 files changed

+64
-55
lines changed

12 files changed

+64
-55
lines changed

‎src/main/java/com/fishercoder/common/classes/ListNode.java‎

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,36 +52,9 @@ public static ListNode createSinglyLinkedList() {
5252
returnhead;
5353
}
5454

55-
publicstaticListNodecreateSinglyLinkedList(List<Integer>listValues) {
56-
if (listValues ==null ||listValues.size() ==0) {
57-
thrownewIllegalArgumentException(
58-
"Please pass in a valid listValues to create a singly linked list.");
59-
}
60-
ListNodehead =newListNode(listValues.get(0));
61-
ListNodetmp =head;
62-
for (inti =1;i <listValues.size();i++) {
63-
ListNodenext =newListNode(listValues.get(i));
64-
tmp.next =next;
65-
tmp =tmp.next;
66-
}
67-
printList(head);
68-
returnhead;
69-
}
70-
71-
publicstaticvoidprintList(ListNodehead) {
72-
ListNodetemp =head;
73-
System.out.println();
74-
while (temp !=null) {
75-
System.out.print(temp.val() +"\t");
76-
temp =temp.next;
77-
}
78-
}
79-
8055
publicstaticvoidmain(String...strings) {
8156
List<Integer>values =CommonUtils.randomIntArrayGenerator(10,20);
82-
createSinglyLinkedList(values);
8357
ListNodehead =createSinglyLinkedList();
84-
printList(head);
8558
System.out.println("The end.");
8659
}
8760

‎src/main/java/com/fishercoder/common/utils/LinkedListUtils.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
importcom.fishercoder.common.classes.ListNode;
44

5+
importjava.util.List;
6+
57
publicclassLinkedListUtils {
68

79
publicstaticListNodecontructLinkedList(int[]nums) {
@@ -17,4 +19,29 @@ public static ListNode contructLinkedList(int[] nums) {
1719
}
1820
returnpre.next;
1921
}
22+
23+
publicstaticvoidprintList(ListNodehead) {
24+
ListNodetemp =head;
25+
System.out.println();
26+
while (temp !=null) {
27+
System.out.print(temp.val() +"\t");
28+
temp =temp.next;
29+
}
30+
}
31+
32+
publicstaticListNodecreateSinglyLinkedList(List<Integer>listValues) {
33+
if (listValues ==null ||listValues.size() ==0) {
34+
thrownewIllegalArgumentException(
35+
"Please pass in a valid listValues to create a singly linked list.");
36+
}
37+
ListNodehead =newListNode(listValues.get(0));
38+
ListNodetmp =head;
39+
for (inti =1;i <listValues.size();i++) {
40+
ListNodenext =newListNode(listValues.get(i));
41+
tmp.next =next;
42+
tmp =tmp.next;
43+
}
44+
printList(head);
45+
returnhead;
46+
}
2047
}

‎src/test/java/com/fishercoder/_1290Test.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
packagecom.fishercoder;
22

33
importcom.fishercoder.common.classes.ListNode;
4+
importcom.fishercoder.common.utils.LinkedListUtils;
45
importcom.fishercoder.solutions._1290;
56
importorg.junit.BeforeClass;
67
importorg.junit.Test;
@@ -20,7 +21,7 @@ public static void setup() {
2021

2122
@Test
2223
publicvoidtest1() {
23-
head =ListNode.createSinglyLinkedList(Arrays.asList(1,0,1));
24+
head =LinkedListUtils.createSinglyLinkedList(Arrays.asList(1,0,1));
2425
assertEquals(5,solution1.getDecimalValue(head));
2526
}
2627

‎src/test/java/com/fishercoder/_21Test.java‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
packagecom.fishercoder;
22

33
importcom.fishercoder.common.classes.ListNode;
4+
importcom.fishercoder.common.utils.LinkedListUtils;
45
importcom.fishercoder.solutions._21;
56
importorg.junit.BeforeClass;
67
importorg.junit.Test;
@@ -23,16 +24,16 @@ public static void setup() {
2324

2425
@Test
2526
publicvoidtest1() {
26-
l1 =ListNode.createSinglyLinkedList(Arrays.asList(1,2,3,5));
27-
l2 =ListNode.createSinglyLinkedList(Arrays.asList(2,4,6));
28-
assertEquals(ListNode.createSinglyLinkedList(Arrays.asList(1,2,2,3,4,5,6)),solution1.mergeTwoLists(l1,l2));
27+
l1 =LinkedListUtils.createSinglyLinkedList(Arrays.asList(1,2,3,5));
28+
l2 =LinkedListUtils.createSinglyLinkedList(Arrays.asList(2,4,6));
29+
assertEquals(LinkedListUtils.createSinglyLinkedList(Arrays.asList(1,2,2,3,4,5,6)),solution1.mergeTwoLists(l1,l2));
2930
}
3031

3132
@Test
3233
publicvoidtest2() {
33-
l1 =ListNode.createSinglyLinkedList(Arrays.asList(1,2,3,5));
34-
l2 =ListNode.createSinglyLinkedList(Arrays.asList(2,4,6));
35-
assertEquals(ListNode.createSinglyLinkedList(Arrays.asList(1,2,2,3,4,5,6)),solution2.mergeTwoLists(l1,l2));
34+
l1 =LinkedListUtils.createSinglyLinkedList(Arrays.asList(1,2,3,5));
35+
l2 =LinkedListUtils.createSinglyLinkedList(Arrays.asList(2,4,6));
36+
assertEquals(LinkedListUtils.createSinglyLinkedList(Arrays.asList(1,2,2,3,4,5,6)),solution2.mergeTwoLists(l1,l2));
3637
}
3738

3839
}

‎src/test/java/com/fishercoder/_237Test.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
importcom.fishercoder.common.classes.ListNode;
44
importcom.fishercoder.common.utils.CommonUtils;
5+
importcom.fishercoder.common.utils.LinkedListUtils;
56
importcom.fishercoder.solutions._237;
67
importorg.junit.BeforeClass;
78
importorg.junit.Test;
@@ -19,7 +20,7 @@ public static void setup() {
1920

2021
@Test
2122
publicvoidtest1() {
22-
head =ListNode.createSinglyLinkedList(Arrays.asList(4,5,1,9));
23+
head =LinkedListUtils.createSinglyLinkedList(Arrays.asList(4,5,1,9));
2324
CommonUtils.printList(head);
2425
solution1.deleteNode(head.next);
2526
CommonUtils.printList(head);

‎src/test/java/com/fishercoder/_23Test.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
importcom.fishercoder.common.classes.ListNode;
44
importcom.fishercoder.common.utils.CommonUtils;
5+
importcom.fishercoder.common.utils.LinkedListUtils;
56
importcom.fishercoder.solutions._23;
67
importorg.junit.BeforeClass;
78
importorg.junit.Test;
@@ -19,9 +20,9 @@ public static void setup() {
1920

2021
@Test
2122
publicvoidtest1() {
22-
ListNodehead1 =ListNode.createSinglyLinkedList(Arrays.asList(1,3,5,7,11));
23-
ListNodehead2 =ListNode.createSinglyLinkedList(Arrays.asList(2,8,12));
24-
ListNodehead3 =ListNode.createSinglyLinkedList(Arrays.asList(4,6,9,10));
23+
ListNodehead1 =LinkedListUtils.createSinglyLinkedList(Arrays.asList(1,3,5,7,11));
24+
ListNodehead2 =LinkedListUtils.createSinglyLinkedList(Arrays.asList(2,8,12));
25+
ListNodehead3 =LinkedListUtils.createSinglyLinkedList(Arrays.asList(4,6,9,10));
2526
lists =newListNode[]{head1,head2,head3};
2627
CommonUtils.printList(solution1.mergeKLists(lists));
2728
}

‎src/test/java/com/fishercoder/_24Test.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
importcom.fishercoder.common.classes.ListNode;
44
importcom.fishercoder.common.utils.CommonUtils;
5+
importcom.fishercoder.common.utils.LinkedListUtils;
56
importcom.fishercoder.solutions._24;
67
importorg.junit.BeforeClass;
78
importorg.junit.Test;
@@ -19,7 +20,7 @@ public static void setup() {
1920

2021
@Test
2122
publicvoidtest1() {
22-
head =ListNode.createSinglyLinkedList(Arrays.asList(1,2,3,4));
23+
head =LinkedListUtils.createSinglyLinkedList(Arrays.asList(1,2,3,4));
2324
CommonUtils.printList(solution1.swapPairs(head));
2425
}
2526

‎src/test/java/com/fishercoder/_725Test.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void test1() {
2525
k =5;
2626
actual =solution1.splitListToParts(root,k);
2727
for (ListNodehead :actual) {
28-
ListNode.printList(head);
28+
LinkedListUtils.printList(head);
2929
}
3030
}
3131

@@ -35,7 +35,7 @@ public void test2() {
3535
k =3;
3636
actual =solution1.splitListToParts(root,k);
3737
for (ListNodehead :actual) {
38-
ListNode.printList(head);
38+
LinkedListUtils.printList(head);
3939
}
4040
}
4141

@@ -45,7 +45,7 @@ public void test3() {
4545
k =5;
4646
actual =solution2.splitListToParts(root,k);
4747
for (ListNodehead :actual) {
48-
ListNode.printList(head);
48+
LinkedListUtils.printList(head);
4949
}
5050
}
5151

@@ -55,7 +55,7 @@ public void test4() {
5555
k =3;
5656
actual =solution2.splitListToParts(root,k);
5757
for (ListNodehead :actual) {
58-
ListNode.printList(head);
58+
LinkedListUtils.printList(head);
5959
}
6060
}
6161

‎src/test/java/com/fishercoder/_83Test.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
packagecom.fishercoder;
22

33
importcom.fishercoder.common.classes.ListNode;
4+
importcom.fishercoder.common.utils.LinkedListUtils;
45
importcom.fishercoder.solutions._83;
56
importjava.util.Arrays;
67
importorg.junit.Assert;
@@ -25,15 +26,15 @@ public static void setup() {
2526

2627
@Test
2728
publicvoidtest1() {
28-
head =ListNode.createSinglyLinkedList(Arrays.asList(1,1,2,3,3));
29-
expected =ListNode.createSinglyLinkedList(Arrays.asList(1,2,3));
29+
head =LinkedListUtils.createSinglyLinkedList(Arrays.asList(1,1,2,3,3));
30+
expected =LinkedListUtils.createSinglyLinkedList(Arrays.asList(1,2,3));
3031
Assert.assertEquals(expected,solution1.deleteDuplicates(head));
3132
}
3233

3334
@Test
3435
publicvoidtest2() {
35-
head =ListNode.createSinglyLinkedList(Arrays.asList(1,1,2,3,3));
36-
expected =ListNode.createSinglyLinkedList(Arrays.asList(1,2,3));
36+
head =LinkedListUtils.createSinglyLinkedList(Arrays.asList(1,1,2,3,3));
37+
expected =LinkedListUtils.createSinglyLinkedList(Arrays.asList(1,2,3));
3738
Assert.assertEquals(expected,solution2.deleteDuplicates(head));
3839
}
3940
}

‎src/test/java/com/fishercoder/_86Test.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
packagecom.fishercoder;
22

33
importcom.fishercoder.common.classes.ListNode;
4+
importcom.fishercoder.common.utils.LinkedListUtils;
45
importcom.fishercoder.solutions._86;
56
importorg.junit.BeforeClass;
67
importorg.junit.Test;
@@ -21,8 +22,8 @@ public static void setup() {
2122

2223
@Test
2324
publicvoidtest1() {
24-
head =ListNode.createSinglyLinkedList(Arrays.asList(1,4,3,2,5,2));
25-
expected =ListNode.createSinglyLinkedList(Arrays.asList(1,2,2,4,3,5));
25+
head =LinkedListUtils.createSinglyLinkedList(Arrays.asList(1,4,3,2,5,2));
26+
expected =LinkedListUtils.createSinglyLinkedList(Arrays.asList(1,2,2,4,3,5));
2627
assertEquals(expected, (solution1.partition(head,3)));
2728
}
2829

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp