Skip to content

Commit cfc5491

Browse files
committed
feat: update leetcode solutions: 0061. Rotate List
1 parent 760635a commit cfc5491

File tree

5 files changed

+160
-47
lines changed

5 files changed

+160
-47
lines changed

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

+58-9
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,11 @@
4848
# self.next = next
4949
class Solution:
5050
def rotateRight(self, head: ListNode, k: int) -> ListNode:
51-
if head is None or head.next is None or k == 0:
51+
if k == 0 or head is None or head.next is None:
5252
return head
53-
n = 0
54-
cur = head
53+
n, cur = 0, head
5554
while cur:
56-
n += 1
57-
cur = cur.next
55+
n, cur = n + 1, cur.next
5856
k %= n
5957
if k == 0:
6058
return head
@@ -86,21 +84,21 @@ class Solution:
8684
*/
8785
class Solution {
8886
public ListNode rotateRight(ListNode head, int k) {
89-
if (head == null || head.next == null) {
87+
if (k == 0 || head == null || head.next == null) {
9088
return head;
9189
}
92-
int n = 0;
9390
ListNode cur = head;
91+
int n = 0;
9492
while (cur != null) {
95-
++n;
9693
cur = cur.next;
94+
++n;
9795
}
9896
k %= n;
9997
if (k == 0) {
10098
return head;
10199
}
102100
ListNode p = head, q = head;
103-
for (int i = 0; i < k; ++i) {
101+
while (k-- > 0) {
104102
q = q.next;
105103
}
106104
while (q.next != null) {
@@ -115,6 +113,57 @@ class Solution {
115113
}
116114
```
117115

116+
### **C#**
117+
118+
```cs
119+
/**
120+
* Definition for singly-linked list.
121+
* public class ListNode {
122+
* public int val;
123+
* public ListNode next;
124+
* public ListNode(int val=0, ListNode next=null) {
125+
* this.val = val;
126+
* this.next = next;
127+
* }
128+
* }
129+
*/
130+
public class Solution {
131+
public ListNode RotateRight(ListNode head, int k) {
132+
if (k == 0 || head == null || head.next == null)
133+
{
134+
return head;
135+
}
136+
ListNode cur = head;
137+
var n = 0;
138+
while (cur != null)
139+
{
140+
cur = cur.next;
141+
++n;
142+
}
143+
k %= n;
144+
if (k == 0)
145+
{
146+
return head;
147+
}
148+
ListNode p = head, q = head;
149+
while (k-- > 0)
150+
{
151+
q = q.next;
152+
}
153+
while (q.next != null)
154+
{
155+
p = p.next;
156+
q = q.next;
157+
}
158+
ListNode start = p.next;
159+
p.next = null;
160+
q.next = head;
161+
return start;
162+
163+
}
164+
}
165+
```
166+
118167
### **...**
119168

120169
```

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

+58-9
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,11 @@ rotate 4 steps to the right:&nbsp;<code>2-&gt;0-&gt;1-&gt;NULL</code></pre>
5454
# self.next = next
5555
class Solution:
5656
def rotateRight(self, head: ListNode, k: int) -> ListNode:
57-
if head is None or head.next is None or k == 0:
57+
if k == 0 or head is None or head.next is None:
5858
return head
59-
n = 0
60-
cur = head
59+
n, cur = 0, head
6160
while cur:
62-
n += 1
63-
cur = cur.next
61+
n, cur = n + 1, cur.next
6462
k %= n
6563
if k == 0:
6664
return head
@@ -90,21 +88,21 @@ class Solution:
9088
*/
9189
class Solution {
9290
public ListNode rotateRight(ListNode head, int k) {
93-
if (head == null || head.next == null) {
91+
if (k == 0 || head == null || head.next == null) {
9492
return head;
9593
}
96-
int n = 0;
9794
ListNode cur = head;
95+
int n = 0;
9896
while (cur != null) {
99-
++n;
10097
cur = cur.next;
98+
++n;
10199
}
102100
k %= n;
103101
if (k == 0) {
104102
return head;
105103
}
106104
ListNode p = head, q = head;
107-
for (int i = 0; i < k; ++i) {
105+
while (k-- > 0) {
108106
q = q.next;
109107
}
110108
while (q.next != null) {
@@ -119,6 +117,57 @@ class Solution {
119117
}
120118
```
121119

120+
### **C#**
121+
122+
```cs
123+
/**
124+
* Definition for singly-linked list.
125+
* public class ListNode {
126+
* public int val;
127+
* public ListNode next;
128+
* public ListNode(int val=0, ListNode next=null) {
129+
* this.val = val;
130+
* this.next = next;
131+
* }
132+
* }
133+
*/
134+
public class Solution {
135+
public ListNode RotateRight(ListNode head, int k) {
136+
if (k == 0 || head == null || head.next == null)
137+
{
138+
return head;
139+
}
140+
ListNode cur = head;
141+
var n = 0;
142+
while (cur != null)
143+
{
144+
cur = cur.next;
145+
++n;
146+
}
147+
k %= n;
148+
if (k == 0)
149+
{
150+
return head;
151+
}
152+
ListNode p = head, q = head;
153+
while (k-- > 0)
154+
{
155+
q = q.next;
156+
}
157+
while (q.next != null)
158+
{
159+
p = p.next;
160+
q = q.next;
161+
}
162+
ListNode start = p.next;
163+
p.next = null;
164+
q.next = head;
165+
return start;
166+
167+
}
168+
}
169+
```
170+
122171
### **...**
123172

124173
```
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,46 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public int val;
5+
* public ListNode next;
6+
* public ListNode(int val=0, ListNode next=null) {
7+
* this.val = val;
8+
* this.next = next;
9+
* }
10+
* }
11+
*/
112
public class Solution {
213
public ListNode RotateRight(ListNode head, int k) {
3-
var length = 0;
4-
var temp = head;
5-
var last = head;
6-
while (temp != null)
14+
if (k == 0 || head == null || head.next == null)
715
{
8-
++length;
9-
last = temp;
10-
temp = temp.next;
16+
return head;
1117
}
12-
if (length == 0) return null;
13-
14-
k %= length;
15-
if (k == 0) return head;
16-
k = length - k;
17-
18-
ListNode kNode = head;
19-
for (var i = 1; i < k; ++i)
18+
ListNode cur = head;
19+
var n = 0;
20+
while (cur != null)
21+
{
22+
cur = cur.next;
23+
++n;
24+
}
25+
k %= n;
26+
if (k == 0)
27+
{
28+
return head;
29+
}
30+
ListNode p = head, q = head;
31+
while (k-- > 0)
32+
{
33+
q = q.next;
34+
}
35+
while (q.next != null)
2036
{
21-
kNode = kNode.next;
37+
p = p.next;
38+
q = q.next;
2239
}
40+
ListNode start = p.next;
41+
p.next = null;
42+
q.next = head;
43+
return start;
2344

24-
last.next = head;
25-
head = kNode.next;
26-
kNode.next = null;
27-
return head;
2845
}
2946
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@
1010
*/
1111
class Solution {
1212
public ListNode rotateRight(ListNode head, int k) {
13-
if (head == null || head.next == null) {
13+
if (k == 0 || head == null || head.next == null) {
1414
return head;
1515
}
16-
int n = 0;
1716
ListNode cur = head;
17+
int n = 0;
1818
while (cur != null) {
19-
++n;
2019
cur = cur.next;
20+
++n;
2121
}
2222
k %= n;
2323
if (k == 0) {
2424
return head;
2525
}
2626
ListNode p = head, q = head;
27-
for (int i = 0; i < k; ++i) {
27+
while (k-- > 0) {
2828
q = q.next;
2929
}
3030
while (q.next != null) {

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
# self.next = next
66
class Solution:
77
def rotateRight(self, head: ListNode, k: int) -> ListNode:
8-
if head is None or head.next is None or k == 0:
8+
if k == 0 or head is None or head.next is None:
99
return head
10-
n = 0
11-
cur = head
10+
n, cur = 0, head
1211
while cur:
13-
n += 1
14-
cur = cur.next
12+
n, cur = n + 1, cur.next
1513
k %= n
1614
if k == 0:
1715
return head

0 commit comments

Comments
 (0)