Skip to content

Commit daef2ee

Browse files
author
Ezra Nyabuti
committed
feat: implement game result logic and enhance stack operations
1 parent ac458a6 commit daef2ee

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,27 @@ 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 oddValue = 0;
11+
int evenValue = 0;
12+
13+
while (head != null && head.next != null) {
14+
int val1 = head.val;
15+
int val2 = head.next.val;
16+
17+
if (val1 < val2) {
18+
oddValue += 1;
19+
} else if (val1 > val2) {
20+
evenValue += 1;
21+
}
22+
head = head.next.next;
23+
}
24+
25+
if (oddValue > evenValue) {
26+
return "Odd";
27+
} else if (evenValue > oddValue) {
28+
return "Even";
29+
} else {
30+
return "Tie";
31+
}
1132
}
1233
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@ 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+
int value = top.val;
19+
top = top.next;
20+
return value;
1721
}
1822

1923
public int peek() {
20-
return 0;
24+
return top.val;
2125
}
2226

2327
public boolean isEmpty() {
24-
return true;
28+
return top == null;
2529
}
2630
}

0 commit comments

Comments
 (0)