diff --git a/lesson_12/structs_ts/src/lesson12.ts b/lesson_12/structs_ts/src/lesson12.ts index d4455564e..1c98c81f1 100644 --- a/lesson_12/structs_ts/src/lesson12.ts +++ b/lesson_12/structs_ts/src/lesson12.ts @@ -6,6 +6,27 @@ export class Lesson12 { * https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game */ public gameResult(head: ListNode | null): string { - return ''; + if (!head) return 'Tie'; + + let current: ListNode | null = head; + const scores: number[] = [0, 0]; + + while (current && current.next) { + if (current.val !== current.next.val && current.val % 2 == 0) { + if (current.val > current.next.val) { + scores[0]++; + } else { + scores[1]++; + } + } + current = current.next; + } + if (scores[0] > scores[1]) { + return 'Even'; + } else if (scores[0] < scores[1]) { + return 'Odd'; + } else { + return 'Tie'; + } } } diff --git a/lesson_12/structs_ts/src/stack.ts b/lesson_12/structs_ts/src/stack.ts index b931ec0ef..f945701a7 100644 --- a/lesson_12/structs_ts/src/stack.ts +++ b/lesson_12/structs_ts/src/stack.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-non-null-assertion */ import { ListNode } from './list_node.js'; export class Stack { @@ -7,19 +8,27 @@ export class Stack { this.top = undefined; } - push(value: number): void { - throw new Error('Not implemented'); + push(val: number): void { + const newNode = new ListNode(val); + newNode.next = this.top; + this.top = newNode; } pop(): number | undefined { - throw new Error('Not implemented'); + if (this.isEmpty()) return undefined; + const val = this.top!.val; + this.top = this.top!.next; + return val; } - peek(): number | null { - throw new Error('Not implemented'); + peek(): number { + if (this.isEmpty()) { + /* empty */ + } + return this.top!.val; } isEmpty(): boolean { - throw new Error('Not implemented'); + return this.top === undefined; } }