diff --git a/lesson_12/structs_ts/src/lesson12.ts b/lesson_12/structs_ts/src/lesson12.ts index d4455564e..02738d730 100644 --- a/lesson_12/structs_ts/src/lesson12.ts +++ b/lesson_12/structs_ts/src/lesson12.ts @@ -6,6 +6,34 @@ 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 ''; + let e_pts = 0; + let o_pts = 0; + + if (head === null) { + return ''; + } + + let curr: ListNode | undefined = head; + + while (curr != null) { + const next: ListNode | undefined = curr.next; + if (next === undefined) { + return ''; + } + + if (curr.val > next?.val) { + e_pts++; + } else if (curr.val < next?.val) { + o_pts++; + } + + curr = next.next; + } + + if (e_pts === o_pts) { + return 'Tie'; + } + + return e_pts > o_pts ? 'Even' : 'Odd'; } } diff --git a/lesson_12/structs_ts/src/stack.ts b/lesson_12/structs_ts/src/stack.ts index b931ec0ef..ad9be231a 100644 --- a/lesson_12/structs_ts/src/stack.ts +++ b/lesson_12/structs_ts/src/stack.ts @@ -8,18 +8,39 @@ export class Stack { } push(value: number): void { - throw new Error('Not implemented'); + const node: ListNode | undefined = new ListNode(value); + + node.next = this.top; + this.top = node; } pop(): number | undefined { - throw new Error('Not implemented'); + if (this.isEmpty()) { + throw new Error('Stack is empty'); + } + + if (this.top) { + const value_to_pop: number = this.top.val; + this.top = this.top.next; + return value_to_pop; + } + + return undefined; } peek(): number | null { - throw new Error('Not implemented'); + if (this.isEmpty()) { + throw new Error('Stack is empty'); + } + + if (this.top) { + return this.top.val; + } + + return null; } isEmpty(): boolean { - throw new Error('Not implemented'); + return this.top === undefined; } }