File tree 3 files changed +121
-0
lines changed
solution/0000-0099/0061.Rotate List
3 files changed +121
-0
lines changed Original file line number Diff line number Diff line change @@ -122,6 +122,48 @@ class Solution {
122
122
}
123
123
```
124
124
125
+ ### ** TypeScript**
126
+
127
+ ``` ts
128
+ /**
129
+ * Definition for singly-linked list.
130
+ * class ListNode {
131
+ * val: number
132
+ * next: ListNode | null
133
+ * constructor(val?: number, next?: ListNode | null) {
134
+ * this.val = (val===undefined ? 0 : val)
135
+ * this.next = (next===undefined ? null : next)
136
+ * }
137
+ * }
138
+ */
139
+
140
+ function rotateRight(head : ListNode | null , k : number ): ListNode | null {
141
+ if (k == 0 || head == null || head .next == null ) return head ;
142
+ // mod n
143
+ let n = 0 ;
144
+ let p = head ;
145
+ while (p != null ) {
146
+ ++ n ;
147
+ p = p .next ;
148
+ }
149
+ k %= n ;
150
+ if (k == 0 ) return head ;
151
+
152
+ let fast = head , slow = head ;
153
+ for (let i = 0 ; i < k ; ++ i ) {
154
+ fast = fast .next ;
155
+ }
156
+ while (fast .next != null ) {
157
+ slow = slow .next ;
158
+ fast = fast .next ;
159
+ }
160
+ let start = slow .next ;
161
+ slow .next = null ;
162
+ fast .next = head ;
163
+ return start ;
164
+ };
165
+ ```
166
+
125
167
### ** C#**
126
168
127
169
``` cs
Original file line number Diff line number Diff line change @@ -108,6 +108,48 @@ class Solution {
108
108
}
109
109
```
110
110
111
+ ### ** TypeScript**
112
+
113
+ ``` ts
114
+ /**
115
+ * Definition for singly-linked list.
116
+ * class ListNode {
117
+ * val: number
118
+ * next: ListNode | null
119
+ * constructor(val?: number, next?: ListNode | null) {
120
+ * this.val = (val===undefined ? 0 : val)
121
+ * this.next = (next===undefined ? null : next)
122
+ * }
123
+ * }
124
+ */
125
+
126
+ function rotateRight(head : ListNode | null , k : number ): ListNode | null {
127
+ if (k == 0 || head == null || head .next == null ) return head ;
128
+ // mod n
129
+ let n = 0 ;
130
+ let p = head ;
131
+ while (p != null ) {
132
+ ++ n ;
133
+ p = p .next ;
134
+ }
135
+ k %= n ;
136
+ if (k == 0 ) return head ;
137
+
138
+ let fast = head , slow = head ;
139
+ for (let i = 0 ; i < k ; ++ i ) {
140
+ fast = fast .next ;
141
+ }
142
+ while (fast .next != null ) {
143
+ slow = slow .next ;
144
+ fast = fast .next ;
145
+ }
146
+ let start = slow .next ;
147
+ slow .next = null ;
148
+ fast .next = head ;
149
+ return start ;
150
+ };
151
+ ```
152
+
111
153
### ** C#**
112
154
113
155
``` cs
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * class ListNode {
4
+ * val: number
5
+ * next: ListNode | null
6
+ * constructor(val?: number, next?: ListNode | null) {
7
+ * this.val = (val===undefined ? 0 : val)
8
+ * this.next = (next===undefined ? null : next)
9
+ * }
10
+ * }
11
+ */
12
+
13
+ function rotateRight ( head : ListNode | null , k : number ) : ListNode | null {
14
+ if ( k == 0 || head == null || head . next == null ) return head ;
15
+ // mod n
16
+ let n = 0 ;
17
+ let p = head ;
18
+ while ( p != null ) {
19
+ ++ n ;
20
+ p = p . next ;
21
+ }
22
+ k %= n ;
23
+ if ( k == 0 ) return head ;
24
+
25
+ let fast = head , slow = head ;
26
+ for ( let i = 0 ; i < k ; ++ i ) {
27
+ fast = fast . next ;
28
+ }
29
+ while ( fast . next != null ) {
30
+ slow = slow . next ;
31
+ fast = fast . next ;
32
+ }
33
+ let start = slow . next ;
34
+ slow . next = null ;
35
+ fast . next = head ;
36
+ return start ;
37
+ } ;
You can’t perform that action at this time.
0 commit comments