File tree 3 files changed +154
-0
lines changed
solution/0000-0099/0025.Reverse Nodes in k-Group
3 files changed +154
-0
lines changed Original file line number Diff line number Diff line change @@ -155,6 +155,59 @@ class Solution {
155
155
}
156
156
```
157
157
158
+ ### ** TypeScript**
159
+
160
+ ``` ts
161
+ /**
162
+ * Definition for singly-linked list.
163
+ * class ListNode {
164
+ * val: number
165
+ * next: ListNode | null
166
+ * constructor(val?: number, next?: ListNode | null) {
167
+ * this.val = (val===undefined ? 0 : val)
168
+ * this.next = (next===undefined ? null : next)
169
+ * }
170
+ * }
171
+ */
172
+
173
+ function reverseKGroup(head : ListNode | null , k : number ): ListNode | null {
174
+ let dummy = new ListNode (0 , head );
175
+ let pre = dummy ;
176
+ // pre->head-> ... ->tail-> next
177
+ while (head != null ) {
178
+ let tail = pre ;
179
+ for (let i= 0 ; i < k ; ++ i ) {
180
+ tail = tail .next ;
181
+ if (tail == null ) {
182
+ return dummy .next ;
183
+ }
184
+ }
185
+ let t = tail .next ;
186
+ [head , tail ] = reverse (head , tail );
187
+ // set next
188
+ pre .next = head ;
189
+ tail .next = t ;
190
+ // set new pre and new head
191
+ pre = tail ;
192
+ head = t ;
193
+ }
194
+ return dummy .next ;
195
+ };
196
+
197
+ function reverse (head : ListNode , tail : ListNode ) {
198
+ let cur = head ;
199
+ let pre = tail .next ;
200
+ // head -> next -> ... -> tail -> pre
201
+ while (pre != tail ) {
202
+ let t = cur .next ;
203
+ cur .next = pre ;
204
+ pre = cur ;
205
+ cur = t ;
206
+ }
207
+ return [tail , head ]
208
+ }
209
+ ```
210
+
158
211
### ** Go**
159
212
160
213
``` go
Original file line number Diff line number Diff line change @@ -142,6 +142,59 @@ class Solution {
142
142
}
143
143
```
144
144
145
+ ### ** TypeScript**
146
+
147
+ ``` ts
148
+ /**
149
+ * Definition for singly-linked list.
150
+ * class ListNode {
151
+ * val: number
152
+ * next: ListNode | null
153
+ * constructor(val?: number, next?: ListNode | null) {
154
+ * this.val = (val===undefined ? 0 : val)
155
+ * this.next = (next===undefined ? null : next)
156
+ * }
157
+ * }
158
+ */
159
+
160
+ function reverseKGroup(head : ListNode | null , k : number ): ListNode | null {
161
+ let dummy = new ListNode (0 , head );
162
+ let pre = dummy ;
163
+ // pre->head-> ... ->tail-> next
164
+ while (head != null ) {
165
+ let tail = pre ;
166
+ for (let i= 0 ; i < k ; ++ i ) {
167
+ tail = tail .next ;
168
+ if (tail == null ) {
169
+ return dummy .next ;
170
+ }
171
+ }
172
+ let t = tail .next ;
173
+ [head , tail ] = reverse (head , tail );
174
+ // set next
175
+ pre .next = head ;
176
+ tail .next = t ;
177
+ // set new pre and new head
178
+ pre = tail ;
179
+ head = t ;
180
+ }
181
+ return dummy .next ;
182
+ };
183
+
184
+ function reverse (head : ListNode , tail : ListNode ) {
185
+ let cur = head ;
186
+ let pre = tail .next ;
187
+ // head -> next -> ... -> tail -> pre
188
+ while (pre != tail ) {
189
+ let t = cur .next ;
190
+ cur .next = pre ;
191
+ pre = cur ;
192
+ cur = t ;
193
+ }
194
+ return [tail , head ]
195
+ }
196
+ ```
197
+
145
198
### ** Go**
146
199
147
200
``` go
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 reverseKGroup ( head : ListNode | null , k : number ) : ListNode | null {
14
+ let dummy = new ListNode ( 0 , head ) ;
15
+ let pre = dummy ;
16
+ // pre->head-> ... ->tail-> next
17
+ while ( head != null ) {
18
+ let tail = pre ;
19
+ for ( let i = 0 ; i < k ; ++ i ) {
20
+ tail = tail . next ;
21
+ if ( tail == null ) {
22
+ return dummy . next ;
23
+ }
24
+ }
25
+ let t = tail . next ;
26
+ [ head , tail ] = reverse ( head , tail ) ;
27
+ // set next
28
+ pre . next = head ;
29
+ tail . next = t ;
30
+ // set new pre and new head
31
+ pre = tail ;
32
+ head = t ;
33
+ }
34
+ return dummy . next ;
35
+ } ;
36
+
37
+ function reverse ( head : ListNode , tail : ListNode ) {
38
+ let cur = head ;
39
+ let pre = tail . next ;
40
+ // head -> next -> ... -> tail -> pre
41
+ while ( pre != tail ) {
42
+ let t = cur . next ;
43
+ cur . next = pre ;
44
+ pre = cur ;
45
+ cur = t ;
46
+ }
47
+ return [ tail , head ]
48
+ }
You can’t perform that action at this time.
0 commit comments