From dfd7afe7d6ca327b1ba28197bb52758cc2417a9f Mon Sep 17 00:00:00 2001 From: haldanek Date: Wed, 23 Oct 2024 01:53:06 +0000 Subject: [PATCH 1/8] feats: adds Kimberlee's Lesson-12: Data Structures: Stacks, Queue, Linked Lists --- .../com/codedifferently/lesson12/Main.java | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Main.java diff --git a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Main.java b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Main.java new file mode 100644 index 000000000..15383e76a --- /dev/null +++ b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Main.java @@ -0,0 +1,94 @@ +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 + */ + class TeamNode { + int data; + TeamNode next; + + public TeamNode(int data) { + this.data = data; + this.next = null; + } + } + + class LinkedList { + TeamNode head; + + public void add(int data) { + TeamNode newTeamNode = new TeamNode(data); + if (head == null) { + head = newTeamNode; + } else { + TeamNode temp = head; + while (temp.next != null) { + temp = temp.next; + } + temp.next = newTeamNode; + } + } + + public String gameResult(TeamNode head) { + List result = new ArrayList<>(); + int evenCounter = 0; + int oddCounter = 0; + TeamNode 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("Nodes with higher values in each pair: " + result); + System.out.println("The team with the most high-values is: " + winningTeam); + + return winningTeam; + } + } +} + +public class Main { + public static void main(String[] args) { + LinkedList list = new LinkedList(); + + // Adding some nodes to the linked list + list.add(2); + list.add(5); + list.add(4); + list.add(7); + list.add(20); + list.add(5); + + // Call the function to compare pairs and determine the group with the most high-value pairs + String winningTeam = list.gameResult(list.head); + + // Output the dominant group + System.out.println("Team with most high-value pairs: " + winningTeam); + } +} From 6b1b0f6214913a3b93b0c727ddefeec39280b6bc Mon Sep 17 00:00:00 2001 From: haldanek Date: Wed, 23 Oct 2024 23:28:50 +0000 Subject: [PATCH 2/8] chore: added a missing semicolon --- .../codedifferently/lesson12/Lesson12.java | 87 ++++++++++++++++++- 1 file changed, 85 insertions(+), 2 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..027bdbb67 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,95 @@ package com.codedifferently.lesson12; +import java.util.LinkedList; +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; + class TeamNode { + int data; + TeamNode next; + + public TeamNode(int data) { + this.data = data; + this.next = null; + } + } + + class LinkedList { + TeamNode head; + + public void add(int data) { + TeamNode newTeamNode = new TeamNode(data); + if (head == null) { + head = newTeamNode; + } else { + TeamNode temp = head; + while (temp.next != null) { + temp = temp.next; + } + temp.next = newTeamNode; + } + } + + public String gameResult(TeamNode head) { + List result = new ArrayList<>(); + int evenCounter = 0; + int oddCounter = 0; + TeamNode 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("Nodes with higher values in each pair: " + result); + System.out.println("The team with the most high-values is: " + winningTeam); + + return winningTeam; + } + } + + +public class Main { + public static void main(String[] args) { + LinkedList list = new LinkedList(); + + // Adding some nodes to the linked list + list.add(2); + list.add(5); + list.add(4); + list.add(7); + list.add(20); + list.add(5); + + // Call the function to compare pairs and determine the group with the most high-value pairs + String winningTeam = list.gameResult(list.head); + + // Output the dominant group + System.out.println("Team with most high-value pairs: " + winningTeam); } } From 14e7a2b6154aa79c3de8dd5b6159b7687bb6cda6 Mon Sep 17 00:00:00 2001 From: haldanek Date: Wed, 23 Oct 2024 23:46:25 +0000 Subject: [PATCH 3/8] feat: adding lesson 12 Linked List Games again --- .../src/main/java/com/codedifferently/lesson12/Lesson12.java | 1 + 1 file changed, 1 insertion(+) 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 027bdbb67..20cc82f42 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 @@ -93,3 +93,4 @@ public static void main(String[] args) { System.out.println("Team with most high-value pairs: " + winningTeam); } } +} From 9c96edc048998ff6799472c62074c4bb9652692c Mon Sep 17 00:00:00 2001 From: haldanek Date: Thu, 24 Oct 2024 00:32:19 +0000 Subject: [PATCH 4/8] feat: adds Kimberlee's ListNode.java file --- .../codedifferently/lesson12/ListNode.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) 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..0eecab9f7 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,39 @@ public class ListNode { this.val = val; this.next = next; } + + +public static String listWinner(ListNode head) { + int evenScore = 0; + int oddScore = 0; + + ListNode current = head; + while (current != null && current.next != null) { + int evenVal = current.val; + int oddVal = current.next.val; + + if (evenVal > oddVal) { + evenScore++; + } else if (oddVal > evenVal) { + oddScore++; + } + + current = current.next.next; + } + + if (evenScore > oddScore) { + return "Even"; + } else if (oddScore > evenScore) { + return "Odd"; + } else { + return "Tie"; + } +} + +public static void main(String[] args) { + ListNode head = new ListNode(2, new ListNode(5, new ListNode(4, new ListNode(7, new ListNode(20, new ListNode(5)))))); + + String winner = listWinner(head); + System.out.println("The winner is: " + winner); } +} \ No newline at end of file From ed5ac51ab7ef0787642b24bd57bc6b2537e6ce40 Mon Sep 17 00:00:00 2001 From: haldanek Date: Thu, 24 Oct 2024 00:33:44 +0000 Subject: [PATCH 5/8] chore: deleted unnecessary file --- .../com/codedifferently/lesson12/Main.java | 94 ------------------- 1 file changed, 94 deletions(-) delete mode 100644 lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Main.java diff --git a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Main.java b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Main.java deleted file mode 100644 index 15383e76a..000000000 --- a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Main.java +++ /dev/null @@ -1,94 +0,0 @@ -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 - */ - class TeamNode { - int data; - TeamNode next; - - public TeamNode(int data) { - this.data = data; - this.next = null; - } - } - - class LinkedList { - TeamNode head; - - public void add(int data) { - TeamNode newTeamNode = new TeamNode(data); - if (head == null) { - head = newTeamNode; - } else { - TeamNode temp = head; - while (temp.next != null) { - temp = temp.next; - } - temp.next = newTeamNode; - } - } - - public String gameResult(TeamNode head) { - List result = new ArrayList<>(); - int evenCounter = 0; - int oddCounter = 0; - TeamNode 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("Nodes with higher values in each pair: " + result); - System.out.println("The team with the most high-values is: " + winningTeam); - - return winningTeam; - } - } -} - -public class Main { - public static void main(String[] args) { - LinkedList list = new LinkedList(); - - // Adding some nodes to the linked list - list.add(2); - list.add(5); - list.add(4); - list.add(7); - list.add(20); - list.add(5); - - // Call the function to compare pairs and determine the group with the most high-value pairs - String winningTeam = list.gameResult(list.head); - - // Output the dominant group - System.out.println("Team with most high-value pairs: " + winningTeam); - } -} From 48f21ebfd73d78b1e2fabe9fe52a8648ffd10fdf Mon Sep 17 00:00:00 2001 From: haldanek Date: Thu, 24 Oct 2024 01:57:57 +0000 Subject: [PATCH 6/8] feat: adds Kimberlee's lesson 12 Stack.java file --- .../com/codedifferently/lesson12/Stack.java | 20 +++++++++++++++---- 1 file changed, 16 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 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; } } + From 612a189f13a368db146875e927e2520a1c037b59 Mon Sep 17 00:00:00 2001 From: haldanek Date: Thu, 24 Oct 2024 13:52:54 +0000 Subject: [PATCH 7/8] fix: edited lesson12.java and ListNode.java files --- .../codedifferently/lesson12/Lesson12.java | 20 +++++++++---------- .../codedifferently/lesson12/ListNode.java | 4 ++-- 2 files changed, 11 insertions(+), 13 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 20cc82f42..ca619debd 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,7 +1,8 @@ package com.codedifferently.lesson12; -import java.util.LinkedList; +/* import java.util.LinkedList; */ import java.util.ArrayList; +/* import java.util.List; */ import java.util.List; public class Lesson12 { @@ -10,7 +11,7 @@ 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 */ - class TeamNode { + /* class TeamNode { int data; TeamNode next; @@ -34,13 +35,13 @@ public void add(int data) { } temp.next = newTeamNode; } - } + } */ - public String gameResult(TeamNode head) { + public String gameResult(/* Team */ListNode head) { List result = new ArrayList<>(); int evenCounter = 0; int oddCounter = 0; - TeamNode current = head; + /* Team */ListNode current = head; while (current != null && current.next != null) { int evenValue = current.data; @@ -66,7 +67,7 @@ public String gameResult(TeamNode head) { winningTeam = "tie"; } - System.out.println("Nodes with higher values in each pair: " + result); + 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; @@ -74,11 +75,10 @@ public String gameResult(TeamNode head) { } -public class Main { +/* public class Main { public static void main(String[] args) { LinkedList list = new LinkedList(); - // Adding some nodes to the linked list list.add(2); list.add(5); list.add(4); @@ -86,11 +86,9 @@ public static void main(String[] args) { list.add(20); list.add(5); - // Call the function to compare pairs and determine the group with the most high-value pairs String winningTeam = list.gameResult(list.head); - // Output the dominant group System.out.println("Team with most high-value pairs: " + 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 0eecab9f7..5cbdfd952 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 @@ -16,7 +16,7 @@ public class ListNode { } -public static String listWinner(ListNode head) { +/* public static String listWinner(ListNode head) { int evenScore = 0; int oddScore = 0; @@ -48,5 +48,5 @@ public static void main(String[] args) { String winner = listWinner(head); System.out.println("The winner is: " + winner); -} +} */ } \ No newline at end of file From 1a6ff0d3f104b95b268948212bd00e7600988e69 Mon Sep 17 00:00:00 2001 From: haldanek Date: Fri, 25 Oct 2024 04:40:33 +0000 Subject: [PATCH 8/8] fix: removed all unused code --- .../codedifferently/lesson12/Lesson12.java | 50 ++----------------- .../codedifferently/lesson12/ListNode.java | 35 ------------- 2 files changed, 4 insertions(+), 81 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 ca619debd..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,8 +1,7 @@ package com.codedifferently.lesson12; -/* import java.util.LinkedList; */ + import java.util.ArrayList; -/* import java.util.List; */ import java.util.List; public class Lesson12 { @@ -11,37 +10,13 @@ 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 */ - /* class TeamNode { - int data; - TeamNode next; - - public TeamNode(int data) { - this.data = data; - this.next = null; - } - } + - class LinkedList { - TeamNode head; - - public void add(int data) { - TeamNode newTeamNode = new TeamNode(data); - if (head == null) { - head = newTeamNode; - } else { - TeamNode temp = head; - while (temp.next != null) { - temp = temp.next; - } - temp.next = newTeamNode; - } - } */ - - public String gameResult(/* Team */ListNode head) { + public String gameResult(ListNode head) { List result = new ArrayList<>(); int evenCounter = 0; int oddCounter = 0; - /* Team */ListNode current = head; + ListNode current = head; while (current != null && current.next != null) { int evenValue = current.data; @@ -75,20 +50,3 @@ public String gameResult(/* Team */ListNode head) { } -/* public class Main { - public static void main(String[] args) { - LinkedList list = new LinkedList(); - - list.add(2); - list.add(5); - list.add(4); - list.add(7); - list.add(20); - list.add(5); - - String winningTeam = list.gameResult(list.head); - - System.out.println("Team with most high-value pairs: " + 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 5cbdfd952..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,39 +14,4 @@ public class ListNode { this.val = val; this.next = next; } - - -/* public static String listWinner(ListNode head) { - int evenScore = 0; - int oddScore = 0; - - ListNode current = head; - while (current != null && current.next != null) { - int evenVal = current.val; - int oddVal = current.next.val; - - if (evenVal > oddVal) { - evenScore++; - } else if (oddVal > evenVal) { - oddScore++; - } - - current = current.next.next; - } - - if (evenScore > oddScore) { - return "Even"; - } else if (oddScore > evenScore) { - return "Odd"; - } else { - return "Tie"; - } -} - -public static void main(String[] args) { - ListNode head = new ListNode(2, new ListNode(5, new ListNode(4, new ListNode(7, new ListNode(20, new ListNode(5)))))); - - String winner = listWinner(head); - System.out.println("The winner is: " + winner); -} */ } \ No newline at end of file