Skip to content

Commit 511e121

Browse files
committed
Feat: adds Pablo Limon-Paredes Lesson 12 Java game result linked list, and class Stack methods
1 parent 8676acd commit 511e121

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

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

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

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,21 @@ public Stack() {
99
}
1010

1111
public void push(int value) {
12-
// Your code here
12+
ListNode tophat = new ListNode(value);
13+
tophat.next = top;
14+
top = tophat;
1315
}
1416

1517
public int pop() {
16-
return 0;
18+
top = top.next;
19+
return top.val;
1720
}
1821

1922
public int peek() {
20-
return 0;
23+
return top.val;
2124
}
2225

2326
public boolean isEmpty() {
24-
return true;
27+
return top == null;
2528
}
2629
}

0 commit comments

Comments
 (0)