-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateList.java
More file actions
132 lines (122 loc) · 3.48 KB
/
Copy pathRotateList.java
File metadata and controls
132 lines (122 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Source : https://leetcode.com/problems/rotate-list/
// Author : cornprincess
// Date : 2021-03-27
/*****************************************************************************************************
*
* Given the head of a linked list, rotate the list to the right by k places.
*
* Example 1:
*
* Input: head = [1,2,3,4,5], k = 2
* Output: [4,5,1,2,3]
*
* Example 2:
*
* Input: head = [0,1,2], k = 4
* Output: [2,0,1]
*
* Constraints:
*
* The number of nodes in the list is in the range [0, 500].
* -100 <= Node.val <= 100
* 0 <= k <= 2 * 109
******************************************************************************************************/
package RotateList;
import common.ListNode;
public class RotateList {
public ListNode rotateRight(ListNode head, int k) {
if (head == null || head.next == null) {
return head;
}
// 计算链表的长度
// 1 2 3 4 5
int length = 0;
ListNode temp = head;
while (temp != null) {
length++;
temp = temp.next;
}
temp = head;
// 计算开始旋转的节点
if (k == length || k == 0) {
return head;
}
int remainder = k % length;
int rotateIndex = length - remainder -1;
for (int i = 0; i < rotateIndex; i++) {
temp = temp.next;
}
// 旋转分为两步
// 截断
ListNode newHead = temp.next;
temp.next = null;
// 链接
temp = newHead;
while (temp.next != null) {
temp = temp.next;
}
temp.next = head;
return newHead;
}
public ListNode rotateRight2(ListNode head, int k) {
if (head == null || head.next == null) {
return head;
}
// 1. 计算链表长度
ListNode temp = head;
// core 注意这里长度初始化为1,并且 while 的判断条件为 temp.next != null,
// core 这样可以是的 temp 在循环结束之后正好是链表的最后一个节点,方便进行成环操作
int length = 1;
while (temp.next != null) {
length++;
temp = temp.next;
}
// 2. 计算断开的节点
int remainder = k % length;
if (remainder == 0) {
return head;
}
int rotateIndex = length - remainder;
// 3. core 闭合成环 这一步一定要在 return head 后面,否则会返回成环的链表
temp.next = head;
// 4. 断开
while (rotateIndex > 0) {
temp = temp.next;
rotateIndex--;
}
ListNode res = temp.next;
temp.next = null;
return res;
}
public ListNode rotateRight3(ListNode head, int k) {
if (head == null || head.next == null) {
return head;
}
// 1.计算断开的节点,即k值
ListNode temp = head;
int length = 0;
while (temp != null) {
length++;
temp = temp.next;
}
k %= length;
if (k == 0) {
return head;
}
// 1 2 3 4 5
ListNode slow = head;
ListNode fast = head;
while (k >0) {
fast = fast.next;
k--;
}
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
ListNode res = slow.next;
slow.next = null;
fast.next = head;
return res;
}
}