File tree Expand file tree Collapse file tree 2 files changed +11
-9
lines changed Expand file tree Collapse file tree 2 files changed +11
-9
lines changed Original file line number Diff line number Diff line change 1
- /* eslint-disable @typescript-eslint/no-unused-vars */
2
1
import { ListNode } from './list_node.js' ;
3
2
4
3
export class Lesson12 {
5
- push ( scores : number ) : void {
6
- throw new Error ( 'Not implemented' ) ;
7
- }
8
4
/**
9
5
* Provide the solution to LeetCode 3062 here:
10
6
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
Original file line number Diff line number Diff line change
1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
1
2
import { ListNode } from './list_node.js' ;
2
3
3
4
export class Stack {
@@ -7,19 +8,24 @@ export class Stack {
7
8
this . top = undefined ;
8
9
}
9
10
10
- push ( number : number ) : void {
11
- throw new Error ( 'Not implemented' ) ;
11
+ push ( value : number ) : void {
12
+ const newNode = new ListNode ( value ) ;
13
+ newNode . next = this . top ;
14
+ this . top = newNode ;
12
15
}
13
16
14
17
pop ( ) : number | undefined {
15
- throw new Error ( 'Not implemented' ) ;
18
+ if ( this . isEmpty ( ) ) return undefined ;
19
+ const value = this . top ! . value ;
20
+ this . top = this . top ! . next ;
21
+ return value ;
16
22
}
17
23
18
24
peek ( ) : number | null {
19
- throw new Error ( 'Not implemented' ) ;
25
+ return this . isEmpty ( ) ? null : this . top ?. value ;
20
26
}
21
27
22
28
isEmpty ( ) : boolean {
23
- throw new Error ( 'Not implemented' ) ;
29
+ return this . top === undefined ;
24
30
}
25
31
}
You can’t perform that action at this time.
0 commit comments