From 50d10be1cc4f4511ab2e276b11f7de2aa025cc4e Mon Sep 17 00:00:00 2001 From: Kotangora1 Date: Tue, 22 Oct 2024 01:55:15 +0000 Subject: [PATCH 1/2] feat: Adding lesson12 implementation of leetcode with stack by Yemi --- .../codedifferently/lesson12/Lesson12.java | 15 +++++++------ .../com/codedifferently/lesson12/Stack.java | 21 ++++++++++++++----- 2 files changed, 25 insertions(+), 11 deletions(-) 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..43a13ebd9 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,15 @@ package com.codedifferently.lesson12; 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 result[] = new int[] {0, 0}; + while (head.next != null) { + if (head.val != head.next.val && head.val % 2 == 0) { + if (head.val > head.next.val) result[head.val % 2] += 1; + else result[head.next.val % 2] += 1; + } + head = head.next; + } + return (result[0] == result[1]) ? "Tie" : (result[0] > result[1] ? "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..f8e02735d 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 @@ -1,6 +1,5 @@ package com.codedifferently.lesson12; -/** Implement the below Stack by providing code for the class methods. */ public class Stack { private ListNode top; @@ -9,18 +8,30 @@ 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 topmostValue = 0; + if (isEmpty()) { + return Integer.parseInt(null); + } else { + topmostValue = top.val; + top = top.next; + return topmostValue; + } } public int peek() { - return 0; + if (isEmpty()) { + return Integer.parseInt(null); + } + return top.val; } public boolean isEmpty() { - return true; + return top == null; } } From 93be321d37c536551e3cff86fe1b9716ed39a863 Mon Sep 17 00:00:00 2001 From: Kotangora1 Date: Tue, 22 Oct 2024 02:01:27 +0000 Subject: [PATCH 2/2] feat: Adding lesson12 implementation of leetcode with stack by Yemi --- .../src/main/java/com/codedifferently/lesson12/Stack.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 f8e02735d..49d6b6c21 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 @@ -16,11 +16,11 @@ public void push(int value) { public int pop() { int topmostValue = 0; if (isEmpty()) { - return Integer.parseInt(null); + return Integer.parseInt(null); } else { - topmostValue = top.val; - top = top.next; - return topmostValue; + topmostValue = top.val; + top = top.next; + return topmostValue; } }