diff --git a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java index af7663e90..43a13ebd9 100644 --- a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java +++ b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java @@ -1,12 +1,15 @@ package com.codedifferently.lesson12; public 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 String gameResult(ListNode head) { - return null; + int result[] = new int[] {0, 0}; + while (head.next != null) { + if (head.val != head.next.val && head.val % 2 == 0) { + if (head.val > head.next.val) result[head.val % 2] += 1; + else result[head.next.val % 2] += 1; + } + head = head.next; + } + return (result[0] == result[1]) ? "Tie" : (result[0] > result[1] ? "Even" : "Odd"); } } diff --git a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Stack.java b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Stack.java index 8444fceca..b3d97d09f 100644 --- a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Stack.java +++ b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Stack.java @@ -1,6 +1,5 @@ package com.codedifferently.lesson12; -/** Implement the below Stack by providing code for the class methods. */ public class Stack { private ListNode top; @@ -9,18 +8,30 @@ public Stack() { } public void push(int value) { - // Your code here + ListNode newNode = new ListNode(value); + newNode.next = top; + top = newNode; } public int pop() { - return 0; + int topmostValue = 0; + if (isEmpty()) { + throw new IllegalStateException("Stack is empty"); + } else { + topmostValue = top.val; + top = top.next; + return topmostValue; + } } public int peek() { - return 0; + if (isEmpty()) { + throw new IllegalStateException("Stack is empty, no value to peek"); + } + return top.val; } public boolean isEmpty() { - return true; + return top == null; } } diff --git a/lesson_12/structs_ts/src/lesson12.ts b/lesson_12/structs_ts/src/lesson12.ts index d4455564e..39a415645 100644 --- a/lesson_12/structs_ts/src/lesson12.ts +++ b/lesson_12/structs_ts/src/lesson12.ts @@ -1,11 +1,22 @@ 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 ''; - } -} + const result: number[] = [0, 0]; + let current: ListNode | null = head; + + while (current && current.next) { + if (current.val !== current.next.val && current.val % 2 === 0) { + if (current.val > current.next.val) { + result[current.val % 2] += 1; + } else { + result[current.next.val % 2] += 1; + } + } + current = current.next; + } + return (result[0] == result[1]) ? 'Tie' : (result[0] > result[1] ? 'Even' : 'Odd'); + } + +} + diff --git a/lesson_12/structs_ts/src/stack.ts b/lesson_12/structs_ts/src/stack.ts index b931ec0ef..2fd6e16b1 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'); + let topmostValue = undefined; + if (this.isEmpty()) + throw new Error('No value to pop'); + else + topmostValue = this.top?.val; + this.top = this.top?.next; + return topmostValue } peek(): number | null { - throw new Error('Not implemented'); + if (this.top == undefined) + throw new Error('No value to pop'); + else + return this.top.val; } isEmpty(): boolean { - throw new Error('Not implemented'); - } + return (this.top == null); + } }