Skip to content

Commit 4516e84

Browse files
committed
Add solution 061 [Java]
1 parent a4985d4 commit 4516e84

File tree

3 files changed

+126
-3
lines changed

3 files changed

+126
-3
lines changed

README.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ Complete solutions to Leetcode problems, updated daily.
1515

1616
## Solution List
1717

18-
### Easy
18+
<detail>
19+
<summary><b>Easy</b></summary>
1920

2021
| # | Title | Tags |
2122
|---|---|---|
@@ -40,8 +41,11 @@ Complete solutions to Leetcode problems, updated daily.
4041
| 703 | [Kth Largest Element in a Stream](https://github.com/doocs/leetcode/tree/master/solution/703.Kth%20Largest%20Element%20in%20a%20Stream) | `Heap` |
4142
| 876 | [Middle of the Linked List](https://github.com/doocs/leetcode/tree/master/solution/876.Middle%20of%20the%20Linked%20List) | `Linked List` |
4243

44+
</detail>
4345

44-
### Medium
46+
47+
<detail>
48+
<summary><b>Medium</b></summary>
4549

4650
| # | Title | Tags |
4751
|---|---|---|
@@ -52,6 +56,7 @@ Complete solutions to Leetcode problems, updated daily.
5256
| 031 | [Next Permutation](https://github.com/doocs/leetcode/tree/master/solution/031.Next%20Permutation) | `Array` |
5357
| 046 | [Permutations](https://github.com/doocs/leetcode/tree/master/solution/046.Permutations) | `Backtracking` |
5458
| 047 | [Permutations II](https://github.com/doocs/leetcode/tree/master/solution/047.Permutations%20II) | `Backtracking` |
59+
| 061 | [Rotate List](https://github.com/doocs/leetcode/tree/master/solution/061.Rotate%20List) | `Linked List`, `Two Pointers` |
5560
| 062 | [Unique Paths](https://github.com/doocs/leetcode/tree/master/solution/062.Unique%20Paths) | `Array`, `Dynamic Programming` |
5661
| 063 | [Unique Paths II](https://github.com/doocs/leetcode/tree/master/solution/063.Unique%20Paths%20II) | `Array`, `Dynamic Programming` |
5762
| 075 | [Sort Colors](https://github.com/doocs/leetcode/tree/master/solution/075.Sort%20Colors) | `Array`, `Two Pointers`, `Sort` |
@@ -67,8 +72,10 @@ Complete solutions to Leetcode problems, updated daily.
6772
| 150 | [Evaluate Reverse Polish Notation](https://github.com/doocs/leetcode/tree/master/solution/150.Evaluate%20Reverse%20Polish%20Notation) | `Stack` |
6873
| 153 | [Find Minimum in Rotated Sorted Array](https://github.com/doocs/leetcode/tree/master/solution/153.Find%20Minimum%20in%20Rotated%20Sorted%20Array) | `Array`, `Binary Search` |
6974

75+
</detail>
7076

71-
### Hard
77+
<detail>
78+
<summary><b>Hard</b></summary>
7279

7380
| # | Title | Tags |
7481
|---|---|---|
@@ -78,6 +85,8 @@ Complete solutions to Leetcode problems, updated daily.
7885
| 145 | [Binary Tree Postorder Traversal](https://github.com/doocs/leetcode/tree/master/solution/145.Binary%20Tree%20Postorder%20Traversal) | `Stack`, `Tree` |
7986
| 295 | [Find Median from Data Stream](https://github.com/doocs/leetcode/tree/master/solution/295.Find%20Median%20from%20Data%20Stream) | `Heap`, `Design` |
8087

88+
</detail>
89+
8190
## Contributions
8291
I'm looking for long-term contributors/partners to this repo! Send me PRs if you're interested! See the following:
8392
- Fork this repository to your own GitHub account and then [clone](https://help.github.com/articles/cloning-a-repository/) it to your local device.

solution/061.Rotate List/README.md

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
## 旋转链表
2+
### 题目描述
3+
4+
给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数。
5+
6+
**示例 1:**
7+
```
8+
输入: 1->2->3->4->5->NULL, k = 2
9+
输出: 4->5->1->2->3->NULL
10+
解释:
11+
向右旋转 1 步: 5->1->2->3->4->NULL
12+
向右旋转 2 步: 4->5->1->2->3->NULL
13+
```
14+
15+
**示例 2:**
16+
```
17+
输入: 0->1->2->NULL, k = 4
18+
输出: 2->0->1->NULL
19+
解释:
20+
向右旋转 1 步: 2->0->1->NULL
21+
向右旋转 2 步: 1->2->0->NULL
22+
向右旋转 3 步: 0->1->2->NULL
23+
向右旋转 4 步: 2->0->1->NULL
24+
```
25+
26+
### 解法
27+
利用双指针`p`,`q`分别指向链表的头部和尾部,题目是右移 k 个位置,右移时,`q`需要指向`q`的前一个位置,似乎不太好做。换种思路,改用左移,右移 k 位相当于左移 len-k 位。循环移位即可。
28+
29+
```java
30+
/**
31+
* Definition for singly-linked list.
32+
* public class ListNode {
33+
* int val;
34+
* ListNode next;
35+
* ListNode(int x) { val = x; }
36+
* }
37+
*/
38+
class Solution {
39+
public ListNode rotateRight(ListNode head, int k) {
40+
if (head == null) {
41+
return head;
42+
}
43+
int len = 1;
44+
ListNode p = head;
45+
ListNode q = head;
46+
ListNode t = p.next;
47+
48+
while (q.next != null) {
49+
++len;
50+
q = q.next;
51+
}
52+
if (len == 1 || k % len == 0) {
53+
return head;
54+
}
55+
56+
k %= len;
57+
58+
// 右移 k 个位置,相当于左移 (len-k) 个位置
59+
k = len - k;
60+
61+
for (int i = 0; i < k; ++i) {
62+
q.next = p;
63+
p.next = null;
64+
q = q.next;
65+
p = t;
66+
t = p.next;
67+
}
68+
69+
return p;
70+
}
71+
}
72+
```
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode rotateRight(ListNode head, int k) {
11+
if (head == null) {
12+
return head;
13+
}
14+
int len = 1;
15+
ListNode p = head;
16+
ListNode q = head;
17+
ListNode t = p.next;
18+
19+
while (q.next != null) {
20+
++len;
21+
q = q.next;
22+
}
23+
if (len == 1 || k % len == 0) {
24+
return head;
25+
}
26+
27+
k %= len;
28+
29+
// 右移 k 个位置,相当于左移 (len-k) 个位置
30+
k = len - k;
31+
32+
for (int i = 0; i < k; ++i) {
33+
q.next = p;
34+
p.next = null;
35+
q = q.next;
36+
p = t;
37+
t = p.next;
38+
}
39+
40+
return p;
41+
}
42+
}

0 commit comments

Comments
 (0)