File tree Expand file tree Collapse file tree 4 files changed +178
-0
lines changed
src/main/java/leetcode/_61_ Expand file tree Collapse file tree 4 files changed +178
-0
lines changed Original file line number Diff line number Diff line change
1
+ ### [ 61\. Rotate List] ( https://leetcode.com/problems/rotate-list/ )
2
+
3
+ Difficulty: ** Medium**
4
+
5
+
6
+ Given a linked list, rotate the list to the right by _ k_ places, where _ k_ is non-negative.
7
+
8
+ ** Example 1:**
9
+
10
+ ```
11
+ Input: 1->2->3->4->5->NULL, k = 2
12
+ Output: 4->5->1->2->3->NULL
13
+ Explanation:
14
+ rotate 1 steps to the right: 5->1->2->3->4->NULL
15
+ rotate 2 steps to the right: 4->5->1->2->3->NULL
16
+ ```
17
+
18
+ ** Example 2:**
19
+
20
+ ```
21
+ Input: 0->1->2->NULL, k = 4
22
+ Output: 2->0->1->NULL
23
+ Explanation:
24
+ rotate 1 steps to the right: 2->0->1->NULL
25
+ rotate 2 steps to the right: 1->2->0->NULL
26
+ rotate 3 steps to the right: 0->1->2->NULL
27
+ rotate 4 steps to the right: 2->0->1->NULL```
28
+
29
+
30
+ #### Solution
31
+
32
+ Language: **Java**
33
+
34
+ ```java
35
+ /**
36
+ * Definition for singly-linked list.
37
+ * public class ListNode {
38
+ * int val;
39
+ * ListNode next;
40
+ * ListNode(int x) { val = x; }
41
+ * }
42
+ */
43
+ class Solution {
44
+ public ListNode rotateRight(ListNode head, int k) {
45
+ if (head == null || head.next == null) {
46
+ return head;
47
+ }
48
+ int length = 0;
49
+ ListNode index = head;
50
+ while (index.next != null) {
51
+ length++;
52
+ index = index.next;
53
+ }
54
+ length++;
55
+ index.next = head; // 构建成循环链表,index 指向 head 的前一个节点
56
+ k = k % length;
57
+ for (int i = 0; i < length - k; i++) {
58
+ index = head;
59
+ head = head.next;
60
+ }
61
+ index.next = null; // 破除循环链表,添加结束 null 值
62
+ return head;
63
+ }
64
+ }
65
+ ```
66
+ ![ ] ( http://ww1.sinaimg.cn/large/006tNc79ly1g50orael7nj31b80qsaeq.jpg )
Original file line number Diff line number Diff line change
1
+ package leetcode ._61_ ;
2
+
3
+ /**
4
+ * Created by zhangbo54 on 2019-03-04.
5
+ */
6
+ public class Main {
7
+ public static void main (String [] args ) {
8
+ Solution solution = new Solution ();
9
+ System .out .println ( solution .isValid ("" ));
10
+ }
11
+ }
12
+
Original file line number Diff line number Diff line change
1
+ package leetcode ._61_ ;
2
+
3
+ import leetcode .common .ListNode ;
4
+
5
+ /**
6
+ * Definition for singly-linked list.
7
+ * public class ListNode {
8
+ * int val;
9
+ * ListNode next;
10
+ * ListNode(int x) { val = x; }
11
+ * }
12
+ */
13
+ class Solution {
14
+ public ListNode rotateRight (ListNode head , int k ) {
15
+ if (head == null || head .next == null ) {
16
+ return head ;
17
+ }
18
+ int length = 0 ;
19
+ ListNode index = head ;
20
+ while (index .next != null ) {
21
+ length ++;
22
+ index = index .next ;
23
+ }
24
+ length ++;
25
+ index .next = head ; // 构建成循环链表,index 指向 head 的前一个节点
26
+ k = k % length ;
27
+ for (int i = 0 ; i < length - k ; i ++) {
28
+ index = head ;
29
+ head = head .next ;
30
+ }
31
+ index .next = null ; // 破除循环链表,添加结束 null 值
32
+ return head ;
33
+ }
34
+ }
Original file line number Diff line number Diff line change
1
+ ### [ 61\. Rotate List] ( https://leetcode.com/problems/rotate-list/ )
2
+
3
+ Difficulty: ** Medium**
4
+
5
+
6
+ Given a linked list, rotate the list to the right by _ k_ places, where _ k_ is non-negative.
7
+
8
+ ** Example 1:**
9
+
10
+ ```
11
+ Input: 1->2->3->4->5->NULL, k = 2
12
+ Output: 4->5->1->2->3->NULL
13
+ Explanation:
14
+ rotate 1 steps to the right: 5->1->2->3->4->NULL
15
+ rotate 2 steps to the right: 4->5->1->2->3->NULL
16
+ ```
17
+
18
+ ** Example 2:**
19
+
20
+ ```
21
+ Input: 0->1->2->NULL, k = 4
22
+ Output: 2->0->1->NULL
23
+ Explanation:
24
+ rotate 1 steps to the right: 2->0->1->NULL
25
+ rotate 2 steps to the right: 1->2->0->NULL
26
+ rotate 3 steps to the right: 0->1->2->NULL
27
+ rotate 4 steps to the right: 2->0->1->NULL```
28
+
29
+
30
+ #### Solution
31
+
32
+ Language: **Java**
33
+
34
+ ```java
35
+ /**
36
+ * Definition for singly-linked list.
37
+ * public class ListNode {
38
+ * int val;
39
+ * ListNode next;
40
+ * ListNode(int x) { val = x; }
41
+ * }
42
+ */
43
+ class Solution {
44
+ public ListNode rotateRight(ListNode head, int k) {
45
+ if (head == null || head.next == null) {
46
+ return head;
47
+ }
48
+ int length = 0;
49
+ ListNode index = head;
50
+ while (index.next != null) {
51
+ length++;
52
+ index = index.next;
53
+ }
54
+ length++;
55
+ index.next = head; // 构建成循环链表,index 指向 head 的前一个节点
56
+ k = k % length;
57
+ for (int i = 0; i < length - k; i++) {
58
+ index = head;
59
+ head = head.next;
60
+ }
61
+ index.next = null; // 破除循环链表,添加结束 null 值
62
+ return head;
63
+ }
64
+ }
65
+ ```
66
+ ![ ] ( http://ww1.sinaimg.cn/large/006tNc79ly1g50orael7nj31b80qsaeq.jpg )
You can’t perform that action at this time.
0 commit comments