File tree 4 files changed +94
-0
lines changed
typescript/Fast and Slow Pointers
4 files changed +94
-0
lines changed Original file line number Diff line number Diff line change
1
+ function happyNumber ( n : number ) : boolean {
2
+ let slow = n , fast = n ;
3
+ while ( true ) {
4
+ slow = getNextNum ( slow ) ;
5
+ fast = getNextNum ( getNextNum ( fast ) ) ;
6
+ if ( fast == 1 )
7
+ return true ;
8
+ // If the fast and slow pointers meet, a cycle is detected.
9
+ // Hence, 'n' is not a happy number.
10
+ else if ( fast == slow )
11
+ return false ;
12
+ }
13
+ }
14
+
15
+ function getNextNum ( x : number ) : number {
16
+ let nextNum = 0 ;
17
+ while ( x > 0 ) {
18
+ // Extract the last digit of 'x'.
19
+ const digit = x % 10 ;
20
+ // Truncate (remove) the last digit from 'x' using floor
21
+ // division.
22
+ x /= 10 ;
23
+ // Add the square of the extracted digit to the sum.
24
+ nextNum += digit ** 2 ;
25
+ }
26
+ return nextNum ;
27
+ }
Original file line number Diff line number Diff line change
1
+ /* Definition of ListNode:
2
+ class ListNode {
3
+ val: number; next: ListNode | null;
4
+ constructor(val: number) {
5
+ this.val = val;
6
+ this.next = null;
7
+ }
8
+ }
9
+ */
10
+
11
+ function linkedListLoop ( head : ListNode ) : boolean {
12
+ let slow : ListNode | null = head ;
13
+ let fast : ListNode | null = head ;
14
+ // Check both 'fast' and 'fast.next' to avoid null pointer
15
+ // exceptions when we perform 'fast.next' and 'fast.next.next'.
16
+ while ( fast !== null && fast . next !== null ) {
17
+ slow = slow ! . next ;
18
+ fast = fast . next . next ! ;
19
+ if ( fast == slow )
20
+ return true ;
21
+ }
22
+ return false ;
23
+ }
Original file line number Diff line number Diff line change
1
+ /* Definition of ListNode:
2
+ class ListNode {
3
+ val: number; next: ListNode | null;
4
+ constructor(val: number) {
5
+ this.val = val;
6
+ this.next = null;
7
+ }
8
+ }
9
+ */
10
+
11
+ function linkedListLoopNaive ( head : ListNode ) : boolean {
12
+ const visited : Set < ListNode > = new Set ( ) ;
13
+ let curr : ListNode | null = head ;
14
+ while ( curr !== null ) {
15
+ // Cycle detected if the current node has already been visited.
16
+ if ( visited . has ( curr ) )
17
+ return true ;
18
+ visited . add ( curr ) ;
19
+ curr = curr . next ;
20
+ }
21
+ return false ;
22
+ }
Original file line number Diff line number Diff line change
1
+ /* Definition of ListNode:
2
+ class ListNode {
3
+ val: number; next: ListNode | null;
4
+ constructor(val: number) {
5
+ this.val = val;
6
+ this.next = null;
7
+ }
8
+ }
9
+ */
10
+
11
+
12
+ function linkedListMidpoint ( head : ListNode ) : ListNode | null {
13
+ let slow : ListNode | null = head ;
14
+ let fast : ListNode | null = head ;
15
+ // When the fast pointer reaches the end of the list, the slow
16
+ // pointer will be at the midpoint of the linked list.
17
+ while ( fast !== null && fast . next !== null ) {
18
+ slow = slow ! . next ;
19
+ fast = fast . next . next ! ;
20
+ }
21
+ return slow ;
22
+ }
You can’t perform that action at this time.
0 commit comments