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..f58b2b223 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,52 @@ package com.codedifferently.lesson12; + +import java.util.ArrayList; +import java.util.List; + 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; + + + public String gameResult(ListNode head) { + List result = new ArrayList<>(); + int evenCounter = 0; + int oddCounter = 0; + ListNode current = head; + + while (current != null && current.next != null) { + int evenValue = current.data; + int oddValue = current.next.data; + + if (evenValue > oddValue) { + result.add("even"); + evenCounter++; + } else { + result.add("odd"); + oddCounter++; + } + + current = current.next.next; + } + + String winningTeam; + if (evenCounter > oddCounter) { + winningTeam = "even"; + } else if (oddCounter > evenCounter) { + winningTeam = "odd"; + } else { + winningTeam = "tie"; + } + + System.out.println("Node team with higher value in each pair: " + result); + System.out.println("The team with the most high-values is: " + winningTeam); + + return winningTeam; + } } -} + + diff --git a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/ListNode.java b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/ListNode.java index bcd6bd73f..50f4965c2 100644 --- a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/ListNode.java +++ b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/ListNode.java @@ -14,4 +14,4 @@ public class ListNode { this.val = val; this.next = next; } -} +} \ No newline at end of file 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..366e1d4f4 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,29 @@ public Stack() { public void push(int value) { // Your code here + ListNode newNode = ListNode(value); + newNode.next = top; + top = newNode; } public int pop() { - return 0; + if (isEmpty()) { + throw new RuntimeException("Stack is empty! Cannot pop."); + } + int value = top.val; + top = top.next; + return value; } public int peek() { - return 0; + if (isEmpty()) { + throw new RuntimeException("Stack is empty! Cannot peek."); + } + return top.val; } - + public boolean isEmpty() { - return true; + return top == null; } } +