Skip to content

Commit 0debe7f

Browse files
fix: lesson_12
1 parent 4572aa4 commit 0debe7f

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,31 @@
22

33
public class Lesson12 {
44

5-
/**
6-
* Provide the solution to LeetCode 3062 here:
7-
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
8-
*/
95
public String gameResult(ListNode head) {
10-
return null;
6+
int evenScore = 0;
7+
int oddScore = 0;
8+
9+
while (head != null && head.next != null) {
10+
int current = head.val;
11+
int next = head.next.val;
12+
13+
if (current != next && current % 2 == 0) {
14+
if (current > next) {
15+
evenScore++;
16+
} else {
17+
oddScore++;
18+
}
19+
}
20+
21+
head = head.next;
22+
}
23+
24+
if (evenScore == oddScore) {
25+
return "Tie";
26+
} else if (evenScore > oddScore) {
27+
return "Even";
28+
} else {
29+
return "Odd";
30+
}
1131
}
1232
}
Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.codedifferently.lesson12;
22

3-
/** Implement the below Stack by providing code for the class methods. */
43
public class Stack {
54
private ListNode top;
65

@@ -9,18 +8,37 @@ public Stack() {
98
}
109

1110
public void push(int value) {
12-
// Your code here
11+
ListNode newNode = new ListNode(value);
12+
newNode.next = top;
13+
top = newNode;
1314
}
1415

1516
public int pop() {
16-
return 0;
17+
if (isEmpty()) {
18+
throw new RuntimeException("Stack Underflow: Cannot pop from an empty stack");
19+
}
20+
int poppedValue = top.val;
21+
top = top.next;
22+
return poppedValue;
1723
}
1824

1925
public int peek() {
20-
return 0;
26+
if (isEmpty()) {
27+
throw new RuntimeException("Stack is empty: Cannot peek");
28+
}
29+
return top.val;
2130
}
2231

2332
public boolean isEmpty() {
24-
return true;
33+
return top == null;
34+
}
35+
36+
public void printStack() {
37+
ListNode current = top;
38+
while (current != null) {
39+
System.out.print(current.val + " -> ");
40+
current = current.next;
41+
}
42+
System.out.println("null");
2543
}
2644
}

0 commit comments

Comments
 (0)