Skip to content

Commit 9ad3f9c

Browse files
refactor 328
1 parent 40eea70 commit 9ad3f9c

File tree

2 files changed

+37
-39
lines changed

2 files changed

+37
-39
lines changed

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,23 @@ You should try to do it in place. The program should run in O(1) space complexit
1919
*/
2020
public class _328 {
2121

22-
public ListNode oddEvenList(ListNode head) {
23-
if (head != null) {
24-
ListNode odd = head;
25-
ListNode even = head.next;
26-
ListNode evenHead = even;
27-
28-
while (even != null && even.next != null) {
29-
odd.next = odd.next.next;
30-
even.next = even.next.next;
31-
odd = odd.next;
32-
even = even.next;
22+
public static class Solution1 {
23+
public ListNode oddEvenList(ListNode head) {
24+
if (head != null) {
25+
ListNode odd = head;
26+
ListNode even = head.next;
27+
ListNode evenHead = even;
28+
29+
while (even != null && even.next != null) {
30+
odd.next = odd.next.next;
31+
even.next = even.next.next;
32+
odd = odd.next;
33+
even = even.next;
34+
}
35+
36+
odd.next = evenHead;
3337
}
34-
35-
odd.next = evenHead;
38+
return head;
3639
}
37-
return head;
3840
}
39-
4041
}

src/test/java/com/fishercoder/_328Test.java

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,29 @@
77

88
import static org.junit.Assert.assertEquals;
99

10-
/**
11-
* Created by stevesun on 5/29/17.
12-
*/
1310
public class _328Test {
14-
private static _328 test;
15-
private static ListNode expected;
16-
private static ListNode node;
11+
private static _328.Solution1 solution1;
12+
private static ListNode expected;
13+
private static ListNode node;
1714

18-
@BeforeClass
19-
public static void setup() {
20-
test = new _328();
21-
}
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _328.Solution1();
18+
}
2219

23-
@Test
24-
public void test1() {
25-
node = new ListNode(1);
26-
node.next = new ListNode(2);
27-
node.next.next = new ListNode(3);
28-
node.next.next.next = new ListNode(4);
29-
node.next.next.next.next = new ListNode(5);
20+
@Test
21+
public void test1() {
22+
node = new ListNode(1);
23+
node.next = new ListNode(2);
24+
node.next.next = new ListNode(3);
25+
node.next.next.next = new ListNode(4);
26+
node.next.next.next.next = new ListNode(5);
3027

31-
expected = new ListNode(1);
32-
expected.next = new ListNode(3);
33-
expected.next.next = new ListNode(5);
34-
expected.next.next.next = new ListNode(2);
35-
expected.next.next.next.next = new ListNode(4);
36-
assertEquals(expected, test.oddEvenList(node));
37-
}
28+
expected = new ListNode(1);
29+
expected.next = new ListNode(3);
30+
expected.next.next = new ListNode(5);
31+
expected.next.next.next = new ListNode(2);
32+
expected.next.next.next.next = new ListNode(4);
33+
assertEquals(expected, solution1.oddEvenList(node));
34+
}
3835
}

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