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..ecc955e9d 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 @@ -7,6 +7,27 @@ public class Lesson12 { * 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[] scores = new int[2]; + + while (head.next != null) { + // Used ChatGpt to help complete this if statement-starting at the logical & operand + if (head.val != head.next.val && head.val % 2 == 0) { + if (head.val > head.next.val) { + scores[head.val % 2]++; + // Gpt help ended at the line above + } else { + scores[head.next.val % 2]++; + } + } + head = head.next; + } + if (scores[0] == scores[1]) { + return "Tie"; + } else if (scores[0] > scores[1]) { + return "Even"; + } else { + return "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..1cb0e24de 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,5 +1,7 @@ package com.codedifferently.lesson12; +import java.util.EmptyStackException; + /** Implement the below Stack by providing code for the class methods. */ public class Stack { private ListNode top; @@ -9,18 +11,28 @@ 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; + if (isEmpty()) { + throw new EmptyStackException(); + } + int value = top.val; + top = top.next; + return value; } public int peek() { - return 0; + if (isEmpty()) { + throw new EmptyStackException(); + } + return top.val; } public boolean isEmpty() { - return true; + return top == null; } }