8000 add 61 · bloomer1/Leetcode@05aec92 · GitHub
[go: up one dir, main page]

Skip to content

Commit 05aec92

Browse files
add 61
1 parent 7e69bc0 commit 05aec92

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ Your ideas/fixes/algorithms are more than welcome!
355355
|65|[Valid Number](https://leetcode.com/problems/valid-number/)|[Solution](../master/src/main/java/com/stevesun/solutions/_65.java)|O(n)|O(1)|Hard|
356356
|64|[Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/)|[Solution](../master/src/main/java/com/stevesun/solutions/MinimumPathSum.java)|O(m*n)|O(m*n)|Medium| DP
357357
|63|[Unique Paths II](https://leetcode.com/problems/unique-paths-ii/)|[Solution](../master/src/main/java/com/stevesun/solutions/UniquePathsII.java)|O(m*n)|O(m*n)|Medium| DP
358+
|61|[Rotate List](https://leetcode.com/problems/rotate-list/)|[Solution](../master/src/main/java/com/stevesun/solutions/_61.java)|O(n)|O(1)|Medium| Linked List
358359
|60|[Permutation Sequence](https://leetcode.com/problems/permutation-sequence/)|[Solution](../master/src/main/java/com/stevesun/solutions/PermutationSequence.java)|?|?|Medium|
359360
|59|[Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/)|[Solution](../master/src/main/java/com/stevesun/solutions/SpiralMatrixII.java)|O(n)|O(n)|Medium|
360361
|58|[Length of Last Word](https://leetcode.com/problems/length-of-last-word/)|[Solution](../master/src/main/java/com/stevesun/solutions/LengthofLastWord.java)|O(n)|O(1)|Easy|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.stevesun.solutions;
2+
3+
import com.stevesun.common.classes.ListNode;
4+
5+
/**
6+
* 61. Rotate List
7+
*
8+
* Given a list, rotate_naive the list to the right by k places, where k is non-negative.
9+
10+
For example:
11+
Given 1->2->3->4->5->NULL and k = 2,
12+
return 4->5->1->2->3->NULL.
13+
*/
14+
public class _61 {
15+
16+
//credit: https://discuss.leetcode.com/topic/26364/clean-java-solution-with-brief-explanation
17+
//link the tail of the linked list to the head to form a circle, then count to find the pint and cut it
18+
public ListNode rotateRight(ListNode head, int k) {
19+
if (head == null) return head;
20+
ListNode copyHead = head;
21+
int len = 1;
22+
while (copyHead.next != null) {
23+
copyHead = copyHead.next;
24+
len++;
25+
}
26+
copyHead.next = head;
27+
for (int i = len - k%len; i > 1; i--) {
28+
head = head.next;
29+
}
30+
copyHead = head.next;
31+
head.next = null;
32+
return copyHead;
33+
}
34+
35+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.stevesun;
2+
3+
import com.stevesun.common.classes.ListNode;
4+
import com.stevesun.solutions._61;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static junit.framework.Assert.assertEquals;
9+
10+
/**
11+
* Created by stevesun on 4/30/17.
12+
*/
13+
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;
19+
20+
@BeforeClass
21+
public static void setup(){
22+
test = new _61();
23+
}
24+
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);
33+
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);
39+
40+
actual = test.rotateRight(head, k);
41+
assertEquals(expected, actual);
42+
}
43+
}

0 commit comments

Comments
 (0)
0