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..f65460afc 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 @@ -2,11 +2,59 @@ 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 score_even = 0; + int score_odd = 0; + int evenNum = 0; + int oddNum = 0; + for (int i = 0; i < getLength(head); i++) { + if (i % 2 == 0) { + ListNode resultNode = getElementAt(head, i); + evenNum = resultNode.val; + } else if (i % 2 == 1) { + ListNode resultNode = getElementAt(head, i); + oddNum = resultNode.val; + if (evenNum > oddNum) { + score_even += 1; + } else if (evenNum < oddNum) { + score_odd += 1; + } + } + } + if (score_even > score_odd) { + return "Even"; + } else if (score_even < score_odd) { + return "Odd"; + } else { + return "Tie"; + } + } + + public ListNode getElementAt(ListNode head, int position) { + ListNode current = head; + int index = 0; + + // Traverse the list until reaching the desired position + while (current != null) { + if (index == position) { + return current; // Return the node at the specified position + } + current = current.next; // Move to the next node + index++; // Increment the index + } + + return null; // Return null if the position is out of bounds + } + + public int getLength(ListNode head) { + int length = 0; + ListNode current = head; + + while (current != null) { + length++; // increment to keep count through each element + current = current.next; // moves on to next element + } + + return length; } } 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..cb33a945c 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 @@ -9,18 +9,27 @@ public Stack() { } public void push(int value) { - // Your code here + ListNode newNode = new ListNode(value, top); + top = newNode; } public int pop() { - return 0; + if (isEmpty()) { + throw new IllegalStateException("Stack is empty"); + } + int value = top.val; + top = top.next; + return value; } public int peek() { - return 0; + if (isEmpty()) { + throw new IllegalStateException("Stack is empty"); + } + 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..75324a701 100644 --- a/lesson_12/structs_ts/src/lesson12.ts +++ b/lesson_12/structs_ts/src/lesson12.ts @@ -1,11 +1,37 @@ 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 scoreEven = 0; + let scoreOdd = 0; + let current: ListNode | null = head; + let index = 0; + + while (current !== null) { + // Check if the index is even + if (index % 2 === 0) { + // Only check nextNode if current is not the last node + const nextNode = current.next; + if (nextNode != undefined) { + if (current.val > nextNode.val) { + scoreEven += 1; + } else if (current.val < nextNode.val) { + scoreOdd += 1; + } + } + } + + // Move to the next node + current = current.next ?? null; + index++; // Increment the index + } + + if (scoreEven > scoreOdd) { + return 'Even'; + } else if (scoreEven < scoreOdd) { + 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..2191d1ce1 100644 --- a/lesson_12/structs_ts/src/stack.ts +++ b/lesson_12/structs_ts/src/stack.ts @@ -1,25 +1,34 @@ import { ListNode } from './list_node.js'; export class Stack { - private top: ListNode | undefined; + private top?: ListNode; constructor() { this.top = undefined; } push(value: number): void { - throw new Error('Not implemented'); + const newNode = new ListNode(value, this.top); + this.top = newNode; } pop(): number | undefined { - throw new Error('Not implemented'); + if (this.isEmpty()) { + throw new Error('Stack is empty'); + } + const value = this.top?.val; // Non-null assertion since we've checked for emptiness + this.top = this.top?.next; // No change needed here, TypeScript allows accessing next + return value; } - peek(): number | null { - throw new Error('Not implemented'); + peek(): number | undefined { + if (this.isEmpty()) { + throw new Error('Stack is empty'); + } + return this.top?.val; // Non-null assertion since we've checked for emptiness } isEmpty(): boolean { - throw new Error('Not implemented'); + return this.top === undefined; // Check for undefined since top is optional } }