Skip to content

Commit 5fdf6ce

Browse files
committed
feat: Adds Nana's Lesson12.java and Stack.java
1 parent bf96654 commit 5fdf6ce

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,30 @@ public class Lesson12 {
77
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
88
*/
99
public String gameResult(ListNode head) {
10-
return null;
10+
int evenTeam = 0;
11+
int oddTeam = 0;
12+
13+
while (head !=null && head.next != null) {
14+
int current = head.val;
15+
int next = head.next.val;
16+
17+
if (current != next && current %2 == 0) {
18+
if (current > next) {
19+
evenTeam++;
20+
} else {
21+
oddTeam++;
22+
}
23+
}
24+
25+
head = head.next;
26+
}
27+
28+
if (evenTeam == oddTeam) {
29+
return "Tie";
30+
} else if (evenTeam > oddTeam) {
31+
return "Even";
32+
} else {
33+
return "Odd";
34+
}
1135
}
1236
}

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

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

1111
public void push(int value) {
12-
// Your code here
12+
ListNode newNode = new ListNode(value);
13+
newNode.next = top;
14+
top = newNode;
1315
}
1416

1517
public int pop() {
16-
return 0;
18+
if (isEmpty()) {
19+
throw new RuntimeException("Stack Underflow: Can't pop from an empty stack");
20+
}
21+
int poppedValue = top.val;
22+
top = top.next;
23+
return poppedValue;
1724
}
1825

1926
public int peek() {
20-
return 0;
27+
if (isEmpty()) {
28+
throw new RuntimeException("Stack is empty: Cannot peek");
29+
}
30+
return top.val;
2131
}
2232

2333
public boolean isEmpty() {
24-
return true;
34+
return top == null;
2535
}
36+
37+
public void printStack() {
38+
ListNode current = top;
39+
while (current != null) {
40+
System.out.print(current.val + " -> ");
41+
current = current.next;
42+
}
43+
System.out.println("null");
44+
}
2645
}

0 commit comments

Comments
 (0)