From 567346dc01f910db49ef0bec6576e22bbbb5bb90 Mon Sep 17 00:00:00 2001 From: Sdunsmore2006 Date: Wed, 23 Oct 2024 16:47:27 +0000 Subject: [PATCH 1/2] Feat: Adds Shawn Dunsmore Jr java/stack. --- .../codedifferently/lesson12/Lesson12.java | 34 ++++++++++---- .../com/codedifferently/lesson12/Stack.java | 44 ++++++++++++------- 2 files changed, 53 insertions(+), 25 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..b9f9d9e08 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 @@ -2,11 +2,29 @@ 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; - } -} + /** + * 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) { + int oddPoints = 0; + int evenPoints = 0; + + for (ListNode current = head; current != null && current.next != null; current = current.next.next) { + if (current.val < current.next.val) { + oddPoints++; + } else if (current.val > current.next.val) { + evenPoints++; + } + } + + if (oddPoints > evenPoints) { + return "Odd"; + } else if (evenPoints > oddPoints) { + return "Even"; + } else { + return "Tie"; + } + } +} \ 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..56c731f9a 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,26 +1,36 @@ package com.codedifferently.lesson12; /** Implement the below Stack by providing code for the class methods. */ -public class Stack { - private ListNode top; + public class Stack { + private ListNode top; - public Stack() { - this.top = null; - } + public Stack() { + this.top = null; + } - public void push(int value) { - // Your code here - } + public void push(int value) { + ListNode newNode = new ListNode(value); + newNode.next = top; + top = newNode; + } - public int pop() { - return 0; - } + public int pop() { + if (isEmpty()) { + throw new IllegalStateException("Stack is empty"); + } + int value = top.val; + top = top.next; + return value; + } - public int peek() { - return 0; - } + public int peek() { + if (isEmpty()) { + throw new IllegalStateException("Stack is empty"); + } + return top.val; + } - public boolean isEmpty() { - return true; - } + public boolean isEmpty() { + return top == null; + } } From 68d267c7d4d35d78cd199c0cc48d99daf1483487 Mon Sep 17 00:00:00 2001 From: Sdunsmore2006 Date: Wed, 23 Oct 2024 17:02:58 +0000 Subject: [PATCH 2/2] Feat: Adds Shawn Dunsmore Jr Added spotlessapply --- .../codedifferently/lesson12/Lesson12.java | 47 ++++++++--------- .../com/codedifferently/lesson12/Stack.java | 50 +++++++++---------- 2 files changed, 49 insertions(+), 48 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 b9f9d9e08..50d1bf8c3 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 @@ -2,29 +2,30 @@ 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 - */ + /** + * 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) { + int oddPoints = 0; + int evenPoints = 0; - public String gameResult(ListNode head) { - int oddPoints = 0; - int evenPoints = 0; - - for (ListNode current = head; current != null && current.next != null; current = current.next.next) { - if (current.val < current.next.val) { - oddPoints++; - } else if (current.val > current.next.val) { - evenPoints++; - } - } + for (ListNode current = head; + current != null && current.next != null; + current = current.next.next) { + if (current.val < current.next.val) { + oddPoints++; + } else if (current.val > current.next.val) { + evenPoints++; + } + } - if (oddPoints > evenPoints) { - return "Odd"; - } else if (evenPoints > oddPoints) { - return "Even"; - } else { - return "Tie"; - } + if (oddPoints > evenPoints) { + return "Odd"; + } else if (evenPoints > oddPoints) { + return "Even"; + } else { + return "Tie"; } -} \ 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 56c731f9a..655eea0b2 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,36 +1,36 @@ package com.codedifferently.lesson12; /** Implement the below Stack by providing code for the class methods. */ - public class Stack { - private ListNode top; +public class Stack { + private ListNode top; - public Stack() { - this.top = null; - } + public Stack() { + this.top = null; + } - public void push(int value) { - ListNode newNode = new ListNode(value); - newNode.next = top; - top = newNode; - } + public void push(int value) { + ListNode newNode = new ListNode(value); + newNode.next = top; + top = newNode; + } - public int pop() { - if (isEmpty()) { - throw new IllegalStateException("Stack is empty"); - } - int value = top.val; - top = top.next; - return value; + public int pop() { + if (isEmpty()) { + throw new IllegalStateException("Stack is empty"); } + int value = top.val; + top = top.next; + return value; + } - public int peek() { - if (isEmpty()) { - throw new IllegalStateException("Stack is empty"); - } - return top.val; + public int peek() { + if (isEmpty()) { + throw new IllegalStateException("Stack is empty"); } + return top.val; + } - public boolean isEmpty() { - return top == null; - } + public boolean isEmpty() { + return top == null; + } }