Skip to content

Commit cc3510f

Browse files
committed
feat: added Zion's GameResult and Stack Results to lesson_12 hw
1 parent 8d6467e commit cc3510f

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

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

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

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)