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; } }