File tree Expand file tree Collapse file tree 2 files changed +30
-5
lines changed
Expand file tree Collapse file tree 2 files changed +30
-5
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+ let even = 0 ;
10+ let odd = 0 ;
11+ let evenNode : ListNode | null = head ;
12+ let oddNode : ListNode | null = head ?. next ?? null ;
13+ while ( evenNode != null && oddNode != null ) {
14+ if ( evenNode . val > oddNode . val ) {
15+ even ++ ;
16+ } else {
17+ odd ++ ;
18+ }
19+ evenNode = evenNode . next ?. next ?? null ;
20+ oddNode = oddNode . next ?. next ?? null ;
21+ }
22+ if ( even > odd ) {
23+ return 'Even' ;
24+ } else if ( odd > even ) {
25+ return 'Odd' ;
26+ } else {
27+ return 'Tie' ;
28+ }
1029 }
1130}
Original file line number Diff line number Diff line change @@ -8,18 +8,24 @@ export class Stack {
88 }
99
1010 push ( value : number ) : void {
11- throw new Error ( 'Not implemented' ) ;
11+ const newNode = new ListNode ( value , this . top ) ;
12+ this . top = newNode ;
1213 }
1314
1415 pop ( ) : number | undefined {
15- throw new Error ( 'Not implemented' ) ;
16+ const value = this . top ?. val ;
17+ this . top = this . top ?. next ;
18+ return value ;
1619 }
1720
1821 peek ( ) : number | null {
19- throw new Error ( 'Not implemented' ) ;
22+ if ( ! this . top ) {
23+ throw new Error ( 'Stack is empty' ) ;
24+ }
25+ return this . top . val ;
2026 }
2127
2228 isEmpty ( ) : boolean {
23- throw new Error ( 'Not implemented' ) ;
29+ return this . top == null ;
2430 }
2531}
You can’t perform that action at this time.
0 commit comments