File tree Expand file tree Collapse file tree 2 files changed +39
-9
lines changed
Expand file tree Collapse file tree 2 files changed +39
-9
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,25 @@ export class Lesson12 {
66 * https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
77 */
88 public gameResult ( head : ListNode | null ) : string {
9- return '' ;
9+ const scores = [ 0 , 0 ] ;
10+
11+ while ( head ?. next != null ) {
12+ if ( head ?. val != head ?. next ?. val && head ?. val % 2 == 0 ) {
13+ if ( head ?. val > head ?. next . val ) {
14+ scores [ head ?. val % 2 ] += 1 ;
15+ } else {
16+ scores [ head ?. next . val % 2 ] += 1 ;
17+ }
18+ }
19+ head = head ?. next ;
20+ }
21+
22+ if ( scores [ 0 ] === scores [ 1 ] ) {
23+ return 'Tie' ;
24+ } else if ( scores [ 0 ] > scores [ 1 ] ) {
25+ return 'Even' ;
26+ } else {
27+ return 'Odd' ;
28+ }
1029 }
1130}
Original file line number Diff line number Diff line change 11import { ListNode } from './list_node.js' ;
22
33export class Stack {
4- private top : ListNode | undefined ;
4+ private top : ListNode | null ;
55
66 constructor ( ) {
7- this . top = undefined ;
7+ this . top = null ;
88 }
99
1010 push ( value : number ) : void {
11- throw new Error ( 'Not implemented' ) ;
11+ const newNode = new ListNode ( value , this . top ?? undefined ) ;
12+ this . top = newNode ;
1213 }
1314
14- pop ( ) : number | undefined {
15- throw new Error ( 'Not implemented' ) ;
15+ pop ( ) : number {
16+ if ( this . isEmpty ( ) ) {
17+ throw new Error ( 'Stack Underflow: Cannot pop from an empty stack' ) ;
18+ }
19+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
20+ const poppedValue = this . top ! . val ;
21+ this . top = this . top ?. next ?? null ;
22+ return poppedValue ;
1623 }
1724
18- peek ( ) : number | null {
19- throw new Error ( 'Not implemented' ) ;
25+ peek ( ) : number {
26+ if ( this . isEmpty ( ) ) {
27+ throw new Error ( 'Stack is empty: Cannot peek' ) ;
28+ }
29+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
30+ return this . top ! . val ;
2031 }
2132
2233 isEmpty ( ) : boolean {
23- throw new Error ( 'Not implemented' ) ;
34+ return this . top === null ;
2435 }
2536}
You can’t perform that action at this time.
0 commit comments