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

Commit599429f

Browse files
add a solution for 143
1 parent17be791 commit599429f

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,43 @@ public void reorderList(ListNode head) {
5151
}
5252
}
5353
}
54+
55+
publicstaticclassSolution2 {
56+
/**
57+
* My completely original solution on 10/25/2021, although not super efficient in time complexity,
58+
* since I keep going through the rest of the list to the end at each iteration, it's accepted on LeetCode.
59+
*/
60+
publicvoidreorderList(ListNodehead) {
61+
intlen =getLen(head);
62+
if (len <=2) {
63+
return;
64+
}
65+
ListNodecurr =head;
66+
for (inti =0;i <len /2;i++) {
67+
ListNodetmp =curr;
68+
ListNodenewHead =curr.next;
69+
while (tmp.next.next !=null) {
70+
tmp =tmp.next;
71+
}
72+
if (tmp ==curr) {
73+
break;
74+
}
75+
ListNodetail =tmp.next;
76+
tmp.next =null;
77+
curr.next =tail;
78+
tail.next =newHead;
79+
curr =newHead;
80+
}
81+
}
82+
83+
privateintgetLen(ListNodehead) {
84+
intlen =0;
85+
ListNodetmp =head;
86+
while (tmp !=null) {
87+
tmp =tmp.next;
88+
len++;
89+
}
90+
returnlen;
91+
}
92+
}
5493
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.classes.ListNode;
4+
importcom.fishercoder.common.utils.LinkedListUtils;
5+
importcom.fishercoder.solutions._143;
6+
importcom.fishercoder.solutions._1862;
7+
importorg.junit.BeforeClass;
8+
importorg.junit.Test;
9+
10+
importjava.util.Arrays;
11+
12+
importstaticorg.junit.Assert.assertEquals;
13+
14+
publicclass_143Test {
15+
privatestatic_143.Solution1solution1;
16+
privatestatic_143.Solution2solution2;
17+
privatestaticListNodehead;
18+
privatestaticListNodeexpected;
19+
20+
@BeforeClass
21+
publicstaticvoidsetup() {
22+
solution1 =new_143.Solution1();
23+
solution2 =new_143.Solution2();
24+
}
25+
26+
@Test
27+
publicvoidtest1() {
28+
head =LinkedListUtils.createSinglyLinkedList(Arrays.asList(1,2,3,4,5,6,7));
29+
expected =LinkedListUtils.createSinglyLinkedList(Arrays.asList(1,7,2,6,3,5,4));
30+
solution1.reorderList(head);
31+
assertEquals(expected,head);
32+
}
33+
34+
@Test
35+
publicvoidtest2() {
36+
head =LinkedListUtils.createSinglyLinkedList(Arrays.asList(1,2,3,4,5,6,7));
37+
expected =LinkedListUtils.createSinglyLinkedList(Arrays.asList(1,7,2,6,3,5,4));
38+
solution2.reorderList(head);
39+
assertEquals(expected,head);
40+
}
41+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp