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..dfae1e06b 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,44 @@ package com.codedifferently.lesson12; public class Lesson12 { + // node1 node2 node3 node4 null /** * 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; + // These vars keep track of the point for the even and odd indicies. + int oddPoints = 0; + int evenPoints = 0; + + /* + * This var will be redeclared throughout the loop and be used + * to point to the node pairs. + */ + ListNode curr = head; + + /** + * This loop will do two comparisons. The 1st comparison is to determine which index has a + * greater value. The 2nd comparison checks which index team is more points + */ + while (curr != null) { + if (curr.val > curr.next.val) { + evenPoints++; + } else if (curr.val < curr.next.val) { + oddPoints++; + } + + // "curr.next.next" allows for the loop to skip over an index + curr = curr.next.next; + } + + if (oddPoints > evenPoints) { + return "Odd"; + } else if (oddPoints < evenPoints) { + return "Even"; + } else { + return "Tie"; + } } } 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..956750e67 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 @@ -10,17 +10,28 @@ public Stack() { public void push(int value) { // Your code here + ListNode newTop = new ListNode(value); + newTop.next = top; + top = newTop; } public int pop() { - return 0; + if (isEmpty()) { + throw new IllegalStateException("Empty"); + } + int oldTopVal = top.val; + top = top.next; + return oldTopVal; } public int peek() { - return 0; + if (isEmpty()) { + throw new IllegalStateException("Empty"); + } + return top.val; } public boolean isEmpty() { - return true; + return top == null; } }