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..12cda1e08 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,25 @@ 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 e_pts = 0; + int o_pts = 0; + + ListNode curr = head; + + while (curr != null) { + ListNode next = curr.next; + if (curr.val > next.val) { + e_pts++; + } else if (curr.val < next.val) { + o_pts++; + } + curr = next.next; + } + + if (e_pts == o_pts) { + return "Tie"; + } + + return e_pts > o_pts ? "Even" : "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..1517c50ee 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,29 @@ public Stack() { } public void push(int value) { - // Your code here + ListNode node = new ListNode(value); + node.next = top; + top = node; } public int pop() { - return 0; + if (isEmpty()) { + throw new IllegalStateException("Stack is empty"); + } + int valToPop = top.val; + top = top.next; + return valToPop; } public int peek() { - return 0; + if (isEmpty()) { + throw new IllegalStateException("Stack is empty"); + } + + return top.val; } public boolean isEmpty() { - return true; + return top == null; } }