File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -294,7 +294,30 @@ var detectCycle = function(head) {
294
294
};
295
295
```
296
296
297
+ TypeScript:
298
+
299
+ ``` typescript
300
+ function detectCycle(head : ListNode | null ): ListNode | null {
301
+ let slowNode: ListNode | null = head ,
302
+ fastNode: ListNode | null = head ;
303
+ while (fastNode !== null && fastNode .next !== null ) {
304
+ slowNode = (slowNode as ListNode ).next ;
305
+ fastNode = fastNode .next .next ;
306
+ if (slowNode === fastNode ) {
307
+ slowNode = head ;
308
+ while (slowNode !== fastNode ) {
309
+ slowNode = (slowNode as ListNode ).next ;
310
+ fastNode = (fastNode as ListNode ).next ;
311
+ }
312
+ return slowNode ;
313
+ }
314
+ }
315
+ return null ;
316
+ };
317
+ ```
318
+
297
319
Swift:
320
+
298
321
``` swift
299
322
class Solution {
300
323
func detectCycle (_ head : ListNode? ) -> ListNode? {
You can’t perform that action at this time.
0 commit comments