Skip to content

Commit d7025aa

Browse files
committed
feat: adds David's Stack and Lesson for Lesson 12
1 parent 439780a commit d7025aa

File tree

4 files changed

+50
-18
lines changed

4 files changed

+50
-18
lines changed

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,30 @@ public class Lesson12 {
66
* Provide the solution to LeetCode 3062 here:
77
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
88
*/
9-
public String gameResult(ListNode head) {
10-
return null;
11-
}
12-
}
9+
public String gameResult(ListNode head) {
10+
int oddPoints = 0;
11+
int evenPoints = 0;
12+
ListNode current = head;
13+
14+
while (current != null && current.next != null) {
15+
int evenValue = current.val;
16+
int oddValue = current.next.val;
17+
18+
if (evenValue > oddValue) {
19+
evenPoints++;
20+
} else if (oddValue > evenValue) {
21+
oddPoints++;
22+
}
23+
24+
current = current.next.next;
25+
}
26+
27+
if (evenPoints > oddPoints) {
28+
return "Even";
29+
} else if (oddPoints > evenPoints) {
30+
return "Odd";
31+
} else {
32+
return "Tie";
33+
}
34+
}
35+
}

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

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

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

15-
public int pop() {
16-
return 0;
17-
}
17+
public int pop() {
18+
if (isEmpty()){
19+
throw new IllegalStateException("Stack has no values");
20+
}
21+
int valToPop = top.val;
22+
top = top.next;
23+
return valToPop;
24+
}
1825

19-
public int peek() {
20-
return 0;
21-
}
26+
public int peek() {
27+
if (isEmpty()){
28+
throw new IllegalStateException("Stack has no values");
29+
}
2230

23-
public boolean isEmpty() {
24-
return true;
25-
}
26-
}
31+
return top.val;
32+
}
33+
34+
public boolean isEmpty() {
35+
return top == null;
36+
}
37+
}

lesson_12/structs_java/structs_app/src/test/java/com/codedifferently/lesson12/Lesson12Test.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.codedifferently.lesson12;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
54
import org.junit.jupiter.api.Test;
65

76
class Lesson12Test {

lesson_12/structs_java/structs_app/src/test/java/com/codedifferently/lesson12/StackTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import static org.junit.jupiter.api.Assertions.assertFalse;
55
import static org.junit.jupiter.api.Assertions.assertThrows;
66
import static org.junit.jupiter.api.Assertions.assertTrue;
7-
87
import org.junit.jupiter.api.Test;
98

109
class StackTest {

0 commit comments

Comments
 (0)