File tree 3 files changed +15
-11
lines changed
typescript/Fast and Slow Pointers
3 files changed +15
-11
lines changed Original file line number Diff line number Diff line change 1
- /* Definition of ListNode:
1
+ import { ListNode } from "./ds" ;
2
+
3
+ /* Definition of ListNode:
2
4
class ListNode {
3
5
val: number; next: ListNode | null;
4
6
constructor(val: number) {
5
7
this.val = val;
6
8
this.next = null;
7
9
}
8
- }
9
- */
10
+ }*/
10
11
11
- function linkedListLoop ( head : ListNode ) : boolean {
12
+ function linkedListLoop ( head : ListNode | null ) : boolean {
12
13
let slow : ListNode | null = head ;
13
14
let fast : ListNode | null = head ;
14
15
// Check both 'fast' and 'fast.next' to avoid null pointer
Original file line number Diff line number Diff line change
1
+ import { ListNode } from "./ds" ;
2
+
1
3
/* Definition of ListNode:
2
4
class ListNode {
3
5
val: number; next: ListNode | null;
4
6
constructor(val: number) {
5
7
this.val = val;
6
8
this.next = null;
7
9
}
8
- }
9
- */
10
+ }*/
11
+
10
12
11
- function linkedListLoopNaive ( head : ListNode ) : boolean {
13
+ function linkedListLoopNaive ( head : ListNode | null ) : boolean {
12
14
const visited : Set < ListNode > = new Set ( ) ;
13
15
let curr : ListNode | null = head ;
14
16
while ( curr !== null ) {
Original file line number Diff line number Diff line change 1
- /* Definition of ListNode:
1
+ import { ListNode } from "./ds" ;
2
+
3
+ /* Definition of ListNode:
2
4
class ListNode {
3
5
val: number; next: ListNode | null;
4
6
constructor(val: number) {
5
7
this.val = val;
6
8
this.next = null;
7
9
}
8
- }
9
- */
10
+ }*/
10
11
11
12
12
- function linkedListMidpoint ( head : ListNode ) : ListNode | null {
13
+ function linkedListMidpoint ( head : ListNode | null ) : ListNode | null {
13
14
let slow : ListNode | null = head ;
14
15
let fast : ListNode | null = head ;
15
16
// When the fast pointer reaches the end of the list, the slow
You can’t perform that action at this time.
0 commit comments