Skip to content

Commit 95ae6d0

Browse files
committed
feat: implement gameResult method and create Stack operations
1 parent ac458a6 commit 95ae6d0

File tree

3 files changed

+61
-6
lines changed

3 files changed

+61
-6
lines changed

lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,59 @@
22

33
public class Lesson12 {
44

5+
// public static Node {
6+
// public int data;
7+
// public Node next;
8+
9+
// public Node(int data) {
10+
// this.data = data;
11+
// }
12+
// }
513
/**
614
* Provide the solution to LeetCode 3062 here:
715
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
816
*/
9-
public String gameResult(ListNode head) {
10-
return null;
17+
public static String gameResult(ListNode head) {
18+
int[] scoreBoard = {0, 0};
19+
20+
while (head.next != null) {
21+
if (head.val != head.next.val && head.val % 2 == 0) {
22+
if (head.val > head.next.val) {
23+
scoreBoard[head.val % 2] += 1;
24+
} else {
25+
scoreBoard[head.next.val % 2] += 1;
26+
}
27+
}
28+
head = head.next; // Moves to the next Node
29+
}
30+
31+
if (scoreBoard[0] == scoreBoard[1]) {
32+
return "Tie";
33+
} else if (scoreBoard[0] > scoreBoard[1]) {
34+
return "Even";
35+
} else {
36+
return "Odd";
37+
}
1138
}
1239
}
40+
41+
// public static Node createLinkedList(int[] arr) {
42+
// if (arr.length == 0) return null;
43+
44+
// Node head = new Node(arr[0]); //Creates the first node and names it head
45+
// Node current = head; //Tracks the current head inside the loop
46+
47+
// for(int i = 1; i < arr.length; ++i) { //Starts a for loop that is at the second element
48+
// current.next = new Node(arr[i]); //creates a new node and links it
49+
// current = current.next;
50+
// }
51+
// return head;
52+
53+
// }
54+
55+
// public static void main(String[] args) {
56+
// int[] arr = {2, 5, 4, 7, 20, 5};
57+
58+
// System.out.println(createLinkedList(arr));
59+
// }
60+
// }

lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/ListNode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public class ListNode {
44
int val;
55
ListNode next;
6+
int data;
67

78
ListNode() {}
89

lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Stack.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,24 @@ public Stack() {
99
}
1010

1111
public void push(int value) {
12-
// Your code here
12+
ListNode newNode = new ListNode(value); // Will create a new node
13+
newNode.next = top; // Point the new node to the top of the Stack
14+
top = newNode; // Changes the top node to become newNode
1315
}
1416

1517
public int pop() {
16-
return 0;
18+
int value = top.val; // gets the value of top and creating it as a variable value that will temporarily
19+
// store it
20+
top = top.next; // Move top to the next position in the stack this instance it would be whatever
21+
// is below the top node.
22+
return value; // returns that temporary saved val varaible and removes from the stack
1723
}
1824

1925
public int peek() {
20-
return 0;
26+
return top.val; // Will return the top val of the stack "peeking in"
2127
}
2228

2329
public boolean isEmpty() {
24-
return true;
30+
return top == null; // returns true is the top of the stack is empty aka null
2531
}
2632
}

0 commit comments

Comments
 (0)