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..8113e9356 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,28 @@ 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) { + ListNode current = head; + int even = 0; + int odd = 0; + while (current != null) { + ListNode nextNode = current.next; + if (current.val < nextNode.val) { + odd++; + } + if (current.val > nextNode.val) { + even++; + } + current = nextNode.next; + } + if (even > odd) { + return "Even"; + } + if (even < odd) { + return "Odd"; + } + if (even == odd) { + return "Tie"; + } return null; } } 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..f158803fc 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,23 @@ public Stack() { } public void push(int value) { - // Your code here + ListNode newNode = new ListNode(value); + newNode.next = top; + top = newNode; } public int pop() { - return 0; + int value = top.val; + top = top.next; + return value; } public int peek() { - return 0; + return top.val; } public boolean isEmpty() { - return true; + return top == null; } } +// I used chatgpt to walk me through the implementation without giving me the answers. diff --git a/lesson_12/structs_ts/src/lesson12.ts b/lesson_12/structs_ts/src/lesson12.ts index d4455564e..94d3281c3 100644 --- a/lesson_12/structs_ts/src/lesson12.ts +++ b/lesson_12/structs_ts/src/lesson12.ts @@ -6,6 +6,28 @@ export class Lesson12 { * https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game */ public gameResult(head: ListNode | null): string { - return ''; + let ListNode, current = head; + let even = 0; + let odd = 0; + while (current != null && current.next != null) { + let next: ListNode | null = null; + if (head === null) { + odd++; + } + if (current.val > nextNode.val) { + even++; + } + current === nextNode.next; + } + if (even > odd) { + return "Even"; + } + if (even < odd) { + return "Odd"; + } + if (even === odd) { + return "Tie"; + } + return null; } }