Skip to content

Commit 5a9cd15

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0061
1 parent 4713d84 commit 5a9cd15

File tree

6 files changed

+182
-27
lines changed

6 files changed

+182
-27
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
1. [排序链表](/solution/0100-0199/0148.Sort%20List/README.md)
6464
1. [反转链表](/solution/0200-0299/0206.Reverse%20Linked%20List/README.md)
6565
1. [重排链表](/solution/0100-0199/0143.Reorder%20List/README.md)
66+
1. [旋转链表](/solution/0000-0099/0061.Rotate%20List/README.md)
6667
1. [回文链表](/solution/0200-0299/0234.Palindrome%20Linked%20List/README.md)
6768
1. [环形链表](/solution/0100-0199/0141.Linked%20List%20Cycle/README.md)
6869
1. [环形链表 II](/solution/0100-0199/0142.Linked%20List%20Cycle%20II/README.md)

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
6161
1. [Sort List](/solution/0100-0199/0148.Sort%20List/README_EN.md)
6262
1. [Reverse Linked List](/solution/0200-0299/0206.Reverse%20Linked%20List/README_EN.md)
6363
1. [Reorder List](/solution/0100-0199/0143.Reorder%20List/README_EN.md)
64+
1. [Rotate List](/solution/0000-0099/0061.Rotate%20List/README_EN.md)
6465
1. [Palindrome Linked List](/solution/0200-0299/0234.Palindrome%20Linked%20List/README_EN.md)
6566
1. [Linked List Cycle](/solution/0100-0199/0141.Linked%20List%20Cycle/README_EN.md)
6667
1. [Linked List Cycle II](/solution/0100-0199/0142.Linked%20List%20Cycle%20II/README_EN.md)

solution/0000-0099/0061.Rotate List/README.md

+69-2
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,89 @@
3030

3131
<!-- 这里可写通用的实现逻辑 -->
3232

33+
将链表右半部分的 k 的节点拼接到 head 即可。
34+
35+
注:k 对链表长度 n 取余,即 `k %= n`
36+
3337
<!-- tabs:start -->
3438

3539
### **Python3**
3640

3741
<!-- 这里可写当前语言的特殊实现逻辑 -->
3842

3943
```python
40-
44+
# Definition for singly-linked list.
45+
# class ListNode:
46+
# def __init__(self, val=0, next=None):
47+
# self.val = val
48+
# self.next = next
49+
class Solution:
50+
def rotateRight(self, head: ListNode, k: int) -> ListNode:
51+
if head is None or head.next is None or k == 0:
52+
return head
53+
n = 0
54+
cur = head
55+
while cur:
56+
n += 1
57+
cur = cur.next
58+
k %= n
59+
if k == 0:
60+
return head
61+
p = q = head
62+
for i in range(k):
63+
q = q.next
64+
while q.next:
65+
p, q = p.next, q.next
66+
start = p.next
67+
p.next = None
68+
q.next = head
69+
return start
4170
```
4271

4372
### **Java**
4473

4574
<!-- 这里可写当前语言的特殊实现逻辑 -->
4675

4776
```java
48-
77+
/**
78+
* Definition for singly-linked list.
79+
* public class ListNode {
80+
* int val;
81+
* ListNode next;
82+
* ListNode() {}
83+
* ListNode(int val) { this.val = val; }
84+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
85+
* }
86+
*/
87+
class Solution {
88+
public ListNode rotateRight(ListNode head, int k) {
89+
if (head == null || head.next == null) {
90+
return head;
91+
}
92+
int n = 0;
93+
ListNode cur = head;
94+
while (cur != null) {
95+
++n;
96+
cur = cur.next;
97+
}
98+
k %= n;
99+
if (k == 0) {
100+
return head;
101+
}
102+
ListNode p = head, q = head;
103+
for (int i = 0; i < k; ++i) {
104+
q = q.next;
105+
}
106+
while (q.next != null) {
107+
p = p.next;
108+
q = q.next;
109+
}
110+
ListNode start = p.next;
111+
p.next = null;
112+
q.next = head;
113+
return start;
114+
}
115+
}
49116
```
50117

51118
### **...**

solution/0000-0099/0061.Rotate List/README_EN.md

+65-2
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,76 @@ rotate 4 steps to the right:&nbsp;<code>2-&gt;0-&gt;1-&gt;NULL</code></pre>
4747
### **Python3**
4848

4949
```python
50-
50+
# Definition for singly-linked list.
51+
# class ListNode:
52+
# def __init__(self, val=0, next=None):
53+
# self.val = val
54+
# self.next = next
55+
class Solution:
56+
def rotateRight(self, head: ListNode, k: int) -> ListNode:
57+
if head is None or head.next is None or k == 0:
58+
return head
59+
n = 0
60+
cur = head
61+
while cur:
62+
n += 1
63+
cur = cur.next
64+
k %= n
65+
if k == 0:
66+
return head
67+
p = q = head
68+
for i in range(k):
69+
q = q.next
70+
while q.next:
71+
p, q = p.next, q.next
72+
start = p.next
73+
p.next = None
74+
q.next = head
75+
return start
5176
```
5277

5378
### **Java**
5479

5580
```java
56-
81+
/**
82+
* Definition for singly-linked list.
83+
* public class ListNode {
84+
* int val;
85+
* ListNode next;
86+
* ListNode() {}
87+
* ListNode(int val) { this.val = val; }
88+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
89+
* }
90+
*/
91+
class Solution {
92+
public ListNode rotateRight(ListNode head, int k) {
93+
if (head == null || head.next == null) {
94+
return head;
95+
}
96+
int n = 0;
97+
ListNode cur = head;
98+
while (cur != null) {
99+
++n;
100+
cur = cur.next;
101+
}
102+
k %= n;
103+
if (k == 0) {
104+
return head;
105+
}
106+
ListNode p = head, q = head;
107+
for (int i = 0; i < k; ++i) {
108+
q = q.next;
109+
}
110+
while (q.next != null) {
111+
p = p.next;
112+
q = q.next;
113+
}
114+
ListNode start = p.next;
115+
p.next = null;
116+
q.next = head;
117+
return start;
118+
}
119+
}
57120
```
58121

59122
### **...**

solution/0000-0099/0061.Rotate List/Solution.java

+20-23
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,37 @@
33
* public class ListNode {
44
* int val;
55
* ListNode next;
6-
* ListNode(int x) { val = x; }
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
79
* }
810
*/
911
class Solution {
1012
public ListNode rotateRight(ListNode head, int k) {
11-
if (head == null) {
13+
if (head == null || head.next == null) {
1214
return head;
1315
}
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;
16+
int n = 0;
17+
ListNode cur = head;
18+
while (cur != null) {
19+
++n;
20+
cur = cur.next;
2221
}
23-
if (len == 1 || k % len == 0) {
22+
k %= n;
23+
if (k == 0) {
2424
return head;
2525
}
26-
27-
k %= len;
28-
29-
// 右移 k 个位置,相当于左移 (len-k) 个位置
30-
k = len - k;
31-
26+
ListNode p = head, q = head;
3227
for (int i = 0; i < k; ++i) {
33-
q.next = p;
34-
p.next = null;
3528
q = q.next;
36-
p = t;
37-
t = p.next;
3829
}
39-
40-
return p;
30+
while (q.next != null) {
31+
p = p.next;
32+
q = q.next;
33+
}
34+
ListNode start = p.next;
35+
p.next = null;
36+
q.next = head;
37+
return start;
4138
}
4239
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def rotateRight(self, head: ListNode, k: int) -> ListNode:
8+
if head is None or head.next is None or k == 0:
9+
return head
10+
n = 0
11+
cur = head
12+
while cur:
13+
n += 1
14+
cur = cur.next
15+
k %= n
16+
if k == 0:
17+
return head
18+
p = q = head
19+
for i in range(k):
20+
q = q.next
21+
while q.next:
22+
p, q = p.next, q.next
23+
start = p.next
24+
p.next = None
25+
q.next = head
26+
return start

0 commit comments

Comments
 (0)