File tree 5 files changed +109
-0
lines changed
5 files changed +109
-0
lines changed Original file line number Diff line number Diff line change @@ -184,6 +184,7 @@ All solutions will be accepted!
184
184
| 836| [ Rectangle Overlap] ( https://leetcode-cn.com/problems/rectangle-overlap/description/ ) | [ java/py/js] ( ./algorithms/RectangleOverlap ) | Easy|
185
185
| 643| [ Maximum Average Subarray I] ( https://leetcode-cn.com/problems/maximum-average-subarray-i/description/ ) | [ java/py/js] ( ./algorithms/MaximumAverageSubarrayI ) | Easy|
186
186
| 189| [ Rotate Array] ( https://leetcode-cn.com/problems/rotate-array/description/ ) | [ java/py/js] ( ./algorithms/RotateArray ) | Easy|
187
+ | 61| [ Rotate List] ( https://leetcode-cn.com/problems/rotate-list/description/ ) | [ java/py/js] ( ./algorithms/RotateList ) | Medium|
187
188
| 687| [ Longest Univalue Path] ( https://leetcode-cn.com/problems/longest-univalue-path/description/ ) | [ java/py/js] ( ./algorithms/LongestUnivaluePath ) | Easy|
188
189
| 28| [ Implement Strstr] ( https://leetcode-cn.com/problems/implement-strstr/description/ ) | [ java/py/js] ( ./algorithms/ImplementStrstr ) | Easy|
189
190
| 459| [ Repeated Substring Pattern] ( https://leetcode-cn.com/problems/repeated-substring-pattern/description/ ) | [ java/py/js] ( ./algorithms/RepeatedSubstringPattern ) | Easy|
Original file line number Diff line number Diff line change
1
+ # Rotate List
2
+ This problem is easy to solve
Original file line number Diff line number Diff line change
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
+ int length = 0 ;
12
+ ListNode p = head ,
13
+ tail = head ;
14
+
15
+ while (p != null ) {
16
+ tail = p ;
17
+ p = p .next ;
18
+ length ++;
19
+ }
20
+
21
+ if (length == 0 || k % length == 0 )
22
+ return head ;
23
+
24
+ k %= length ;
25
+ tail .next = head ;
26
+ p = head ;
27
+
28
+ for (int i = 0 ; i ++ < length - k - 1 ;)
29
+ p = p .next ;
30
+
31
+ ListNode temp = p .next ;
32
+ p .next = null ;
33
+ return temp ;
34
+ }
35
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * function ListNode(val) {
4
+ * this.val = val;
5
+ * this.next = null;
6
+ * }
7
+ */
8
+ /**
9
+ * @param {ListNode } head
10
+ * @param {number } k
11
+ * @return {ListNode }
12
+ */
13
+ var rotateRight = function ( head , k ) {
14
+ let length = 0 ,
15
+ p = head ,
16
+ tail = head
17
+
18
+ while ( p ) {
19
+ tail = p
20
+ p = p . next
21
+ length ++
22
+ }
23
+
24
+ if ( length == 0 || k % length == 0 )
25
+ return head
26
+
27
+ k %= length
28
+ tail . next = head
29
+ p = head
30
+
31
+ for ( let i = 0 ; i ++ < length - k - 1 ; )
32
+ p = p . next
33
+
34
+ let temp = p . next
35
+ p . next = null
36
+ return temp
37
+ } ;
Original file line number Diff line number Diff line change
1
+ # Definition for singly-linked list.
2
+ # class ListNode(object):
3
+ # def __init__(self, x):
4
+ # self.val = x
5
+ # self.next = None
6
+
7
+ class Solution (object ):
8
+ def rotateRight (self , head , k ):
9
+ """
10
+ :type head: ListNode
11
+ :type k: int
12
+ :rtype: ListNode
13
+ """
14
+ length = 0
15
+ p = head
16
+ tail = p
17
+ while p :
18
+ tail = p
19
+ p = p .next
20
+ length += 1
21
+
22
+ if length == 0 or k % length == 0 :
23
+ return head
24
+ k %= length
25
+ tail .next = head
26
+
27
+ p = head
28
+ for i in xrange (length - k - 1 ):
29
+ p = p .next
30
+
31
+ temp = p .next
32
+ p .next = None
33
+
34
+ return temp
You can’t perform that action at this time.
0 commit comments