Skip to content

Commit cdb9da0

Browse files
refactor 61
1 parent 0e10296 commit cdb9da0

File tree

2 files changed

+43
-45
lines changed

2 files changed

+43
-45
lines changed

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,26 @@
1313
*/
1414
public class _61 {
1515

16+
public static class Solution1 {
1617
//credit: https://discuss.leetcode.com/topic/26364/clean-java-solution-with-brief-explanation
1718
//link the tail of the linked list to the head to form a circle, then count to find the pint and cut it
1819
public ListNode rotateRight(ListNode head, int k) {
19-
if (head == null) {
20-
return head;
21-
}
22-
ListNode copyHead = head;
23-
int len = 1;
24-
while (copyHead.next != null) {
25-
copyHead = copyHead.next;
26-
len++;
27-
}
28-
copyHead.next = head;//link the tail and head to make it a circle
29-
for (int i = len - k % len; i > 1; i--) {
30-
head = head.next;
31-
}
32-
copyHead = head.next;
33-
head.next = null;//break the circle
34-
return copyHead;
20+
if (head == null) {
21+
return head;
22+
}
23+
ListNode copyHead = head;
24+
int len = 1;
25+
while (copyHead.next != null) {
26+
copyHead = copyHead.next;
27+
len++;
28+
}
29+
copyHead.next = head;//link the tail and head to make it a circle
30+
for (int i = len - k % len; i > 1; i--) {
31+
head = head.next;
32+
}
33+
copyHead = head.next;
34+
head.next = null;//break the circle
35+
return copyHead;
3536
}
36-
37+
}
3738
}

src/test/java/com/fishercoder/_61Test.java

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,34 @@
77

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

10-
/**
11-
* Created by fishercoder on 4/30/17.
12-
*/
1310
public class _61Test {
14-
private static _61 test;
15-
private static ListNode expected;
16-
private static ListNode actual;
17-
private static ListNode head;
18-
private static int k;
11+
private static _61.Solution1 solution1;
12+
private static ListNode expected;
13+
private static ListNode actual;
14+
private static ListNode head;
15+
private static int k;
1916

20-
@BeforeClass
21-
public static void setup() {
22-
test = new _61();
23-
}
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _61.Solution1();
20+
}
2421

25-
@Test
26-
public void test1() {
27-
k = 2;
28-
expected = new ListNode(4);
29-
expected.next = new ListNode(5);
30-
expected.next.next = new ListNode(1);
31-
expected.next.next.next = new ListNode(2);
32-
expected.next.next.next.next = new ListNode(3);
22+
@Test
23+
public void test1() {
24+
k = 2;
25+
expected = new ListNode(4);
26+
expected.next = new ListNode(5);
27+
expected.next.next = new ListNode(1);
28+
expected.next.next.next = new ListNode(2);
29+
expected.next.next.next.next = new ListNode(3);
3330

34-
head = new ListNode(1);
35-
head.next = new ListNode(2);
36-
head.next.next = new ListNode(3);
37-
head.next.next.next = new ListNode(4);
38-
head.next.next.next.next = new ListNode(5);
31+
head = new ListNode(1);
32+
head.next = new ListNode(2);
33+
head.next.next = new ListNode(3);
34+
head.next.next.next = new ListNode(4);
35+
head.next.next.next.next = new ListNode(5);
3936

40-
actual = test.rotateRight(head, k);
41-
assertEquals(expected, actual);
42-
}
37+
actual = solution1.rotateRight(head, k);
38+
assertEquals(expected, actual);
39+
}
4340
}

0 commit comments

Comments
 (0)