File tree Expand file tree Collapse file tree 2 files changed +28
-5
lines changed Expand file tree Collapse file tree 2 files changed +28
-5
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,22 @@ export class Lesson12 {
6
6
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
7
7
*/
8
8
public gameResult ( head : ListNode | null ) : string {
9
- return '' ;
9
+ let even = 0 ;
10
+ let odd = 0 ;
11
+ while ( head != null && head . next != null ) {
12
+ if ( head . val > head . next . val ) {
13
+ even ++ ;
14
+ } else {
15
+ odd ++ ;
16
+ }
17
+ head = head . next . next !== undefined ? head . next . next : null ;
18
+ }
19
+ if ( even === odd ) {
20
+ return 'Tie' ;
21
+ } else if ( even > odd ) {
22
+ return 'Even' ;
23
+ } else {
24
+ return 'Odd' ;
25
+ }
10
26
}
11
27
}
Original file line number Diff line number Diff line change @@ -8,18 +8,25 @@ export class Stack {
8
8
}
9
9
10
10
push ( value : number ) : void {
11
- throw new Error ( 'Not implemented' ) ;
11
+ const newnode = new ListNode ( value ) ;
12
+ newnode . next = this . top ;
13
+ this . top = newnode ;
12
14
}
13
15
14
16
pop ( ) : number | undefined {
15
- throw new Error ( 'Not implemented' ) ;
17
+ const nodevalue = this . top ?. val ;
18
+ this . top = this . top ?. next ;
19
+ return nodevalue ;
16
20
}
17
21
18
22
peek ( ) : number | null {
19
- throw new Error ( 'Not implemented' ) ;
23
+ if ( this . top !== null && this . top !== undefined ) {
24
+ return this . top . val ;
25
+ }
26
+ return null ;
20
27
}
21
28
22
29
isEmpty ( ) : boolean {
23
- throw new Error ( 'Not implemented' ) ;
30
+ return this . top == null ;
24
31
}
25
32
}
You can’t perform that action at this time.
0 commit comments