Skip to content

Commit 9c63e5f

Browse files
committed
feat: added gameResult and Stack methods for Chelsea
1 parent 8676acd commit 9c63e5f

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

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

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

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,28 @@ 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 IllegalStateException("Stack is empty");
20+
}
21+
int value = top.val;
22+
top = top.next;
23+
return value;
1724
}
1825

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

2333
public boolean isEmpty() {
24-
return true;
34+
return top == null;
2535
}
2636
}

0 commit comments

Comments
 (0)