File tree Expand file tree Collapse file tree 1 file changed +16
-3
lines changed Expand file tree Collapse file tree 1 file changed +16
-3
lines changed Original file line number Diff line number Diff line change @@ -9,30 +9,43 @@ export class Stack {
9
9
10
10
push ( value : number ) : void {
11
11
// ListNode newNode = new ListNode(value);
12
+ const newNode = new ListNode ( value )
12
13
// newNode.next = top;
14
+ newNode . next = this . top ;
13
15
// top = newNode;
16
+ this . top = newNode ;
14
17
}
15
18
16
19
pop ( ) : number | undefined {
17
- return 0 ;
18
20
// if (this.isEmpty()) {
19
21
// throw new EmptyStackException();
20
22
// } else {
21
23
// int value = top.val;
22
24
// top = top.next;
23
25
// return value;
24
26
// }
27
+ if ( this . isEmpty ( ) ) {
28
+ return undefined ;
29
+ }
30
+
31
+ const value = this . top ?. val ;
32
+ this . top = this . top . next ;
33
+ return value ;
25
34
}
26
35
27
36
peek ( ) : number | null {
28
- return 0 ;
29
37
// if (this.isEmpty()) {
30
38
// throw new EmptyStackException();
31
39
// }
32
40
// return top.val;
41
+ if ( this . isEmpty ( ) ) {
42
+ return undefined ;
43
+ }
44
+
45
+ return this . top ?. val ;
33
46
}
34
47
35
48
isEmpty ( ) : boolean {
36
- return top == null ;
49
+ return this . top === undefined ;
37
50
}
38
51
}
You can’t perform that action at this time.
0 commit comments