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..0bb2e826f 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,35 @@ 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 evenCounter = 0; // Keeps track of even score + int oddCounter = 0; // Keeps track of odd score + + while (head != null + && head.next != null) { // Goes through the Linked List until theres no more pairs + int evenValue = + head.val; // Makes the first even value to compare equal to the first number of the list + int oddValue = + head.next + .val; // Makes the first odd value to compare equal to the second number of the list + + if (evenValue > oddValue) { + evenCounter++; + // Compares the even and odd value, adds 1 to even score if even is greater + } else if (evenValue < oddValue) { + oddCounter++; + } // Compares the even and odd value, adds 1 to odd score if odd is greater + head = head.next.next; // Brings out the next pair befor restarting the loop + } + + if (evenCounter > oddCounter) { + return "Even"; + // Compares the even and odd score, prints 'Even' if Evens score is greater + } + if (evenCounter < oddCounter) { + return "Odd"; + // Compares the even and odd score, prints 'Odd' if odds score is greater + } else { + return "Tie"; + } // Compares the even and odd score, prints 'Tie' if the two scores are equal } } 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..1e3c614c5 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,30 @@ public Stack() { } public void push(int value) { - // Your code here + var newTop = new ListNode(value); + newTop.next = top; + top = newTop; } public int pop() { - return 0; + int newTop = 0; + if (isEmpty()) { + throw new IllegalStateException("Stack is empty"); + } else { + newTop = top.val; + top = top.next; + return newTop; + } } public int peek() { - return 0; + if (isEmpty()) { + throw new IllegalStateException("Stack is empty"); + } + return top.val; } public boolean isEmpty() { - return true; + return top == null; } }