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

Commit81dacbe

Browse files
add a solution for 86
1 parent9461a37 commit81dacbe

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

‎src/main/java/com/fishercoder/solutions/_86.java

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

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

5+
importjava.util.ArrayList;
6+
importjava.util.List;
7+
58
publicclass_86 {
69
publicstaticclassSolution1 {
710
publicListNodepartition(ListNodehead,intx) {
@@ -27,4 +30,32 @@ public ListNode partition(ListNode head, int x) {
2730
returnleft.next;
2831
}
2932
}
33+
34+
publicstaticclassSolution2 {
35+
publicListNodepartition(ListNodehead,intx) {
36+
List<Integer>first =newArrayList<>();
37+
List<Integer>last =newArrayList<>();
38+
while (head !=null) {
39+
if (head.val <x) {
40+
first.add(head.val);
41+
}else {
42+
last.add(head.val);
43+
}
44+
head =head.next;
45+
}
46+
ListNodepre =newListNode(-1);
47+
ListNodetmp =pre;
48+
inti =1;
49+
intj =0;
50+
while (i <first.size() ||j <last.size()) {
51+
if (i <first.size()) {
52+
tmp.next =newListNode(first.get(i++));
53+
}elseif (j <last.size()) {
54+
tmp.next =newListNode(last.get(j++));
55+
}
56+
tmp =tmp.next;
57+
}
58+
returnpre.next;
59+
}
60+
}
3061
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,22 @@
1212

1313
publicclass_86Test {
1414
privatestatic_86.Solution1solution1;
15+
privatestatic_86.Solution2solution2;
1516
privatestaticListNodehead;
1617
privatestaticListNodeexpected;
1718

1819
@BeforeClass
1920
publicstaticvoidsetup() {
2021
solution1 =new_86.Solution1();
22+
solution2 =new_86.Solution2();
2123
}
2224

2325
@Test
2426
publicvoidtest1() {
2527
head =LinkedListUtils.createSinglyLinkedList(Arrays.asList(1,4,3,2,5,2));
2628
expected =LinkedListUtils.createSinglyLinkedList(Arrays.asList(1,2,2,4,3,5));
2729
assertEquals(expected, (solution1.partition(head,3)));
30+
assertEquals(expected, (solution2.partition(head,3)));
2831
}
2932

3033
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp