Skip to content

Commit 97f22cc

Browse files
committed
feat: implement game result logic and enhance stack operations
1 parent ac458a6 commit 97f22cc

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ 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 odd = 0, even = 0;
11+
for (; head != null; head = head.next.next) {
12+
int a = head.val;
13+
int b = head.next.val;
14+
odd += a < b ? 1 : 0;
15+
even += a > b ? 1 : 0;
16+
}
17+
if (odd > even) {
18+
return "Odd";
19+
}
20+
if (odd < even) {
21+
return "Even";
22+
}
23+
return "Tie";
1124
}
1225
}

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,40 @@ public Stack() {
1010

1111
public void push(int value) {
1212
// Your code here
13+
ListNode newNode = new ListNode(value);
14+
if (top == null) {
15+
top = newNode;
16+
} else {
17+
ListNode temp = top;
18+
top = newNode;
19+
top.next = temp;
20+
}
1321
}
1422

1523
public int pop() {
16-
return 0;
24+
if (top != null) {
25+
int value = top.val;
26+
top = top.next;
27+
return value;
28+
} else {
29+
return -1;
30+
}
1731
}
1832

1933
public int peek() {
20-
return 0;
34+
if (top != null) {
35+
return top.val;
36+
} else {
37+
throw new RuntimeException("Stack is empty");
38+
39+
}
2140
}
2241

2342
public boolean isEmpty() {
24-
return true;
43+
if (top == null) {
44+
return true;
45+
} else {
46+
return false;
47+
}
2548
}
2649
}

0 commit comments

Comments
 (0)