diff --git a/lesson_12/structs_ts/src/lesson12.ts b/lesson_12/structs_ts/src/lesson12.ts index d4455564e..678f45a5c 100644 --- a/lesson_12/structs_ts/src/lesson12.ts +++ b/lesson_12/structs_ts/src/lesson12.ts @@ -1,11 +1,26 @@ import { ListNode } from './list_node.js'; export class Lesson12 { - /** - * Provide the solution to LeetCode 3062 here: - * https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game - */ + public gameResult(head: ListNode | null): string { - return ''; + let [oddPoints, evenPoints] = [0,0]; + let current = head; + + while (current != null && current.next != null) { + if (current.val > current.next.val) { + evenPoints++; + } else if (current.val < current.next.val) { + oddPoints++; + } + current = current.next ? current.next.next || null : null; + } + + if (oddPoints > evenPoints) { + return 'Odd'; + } else if (evenPoints > oddPoints) { + return 'Even'; + } else { + return 'Tie'; + } } } diff --git a/lesson_12/structs_ts/src/stack.ts b/lesson_12/structs_ts/src/stack.ts index b931ec0ef..33d88e963 100644 --- a/lesson_12/structs_ts/src/stack.ts +++ b/lesson_12/structs_ts/src/stack.ts @@ -8,18 +8,29 @@ export class Stack { } push(value: number): void { - throw new Error('Not implemented'); + const newNode = new ListNode(value) + newNode.next = this.top; + this.top = newNode; } pop(): number | undefined { - throw new Error('Not implemented'); + if (this.isEmpty()) { + return undefined; + } + + const value = this.top?.val; + this.top = this.top?.next; + return value; } peek(): number | null { - throw new Error('Not implemented'); + if (this.top === undefined) { + throw new Error('Stack is empty'); + } + return this.top.val; } isEmpty(): boolean { - throw new Error('Not implemented'); + return this.top === undefined; } }