Skip to content

Commit bf6dee4

Browse files
feat: added Stack impl/ LeetCode 3062 finished in Java
1 parent 439780a commit bf6dee4

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

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

Lines changed: 15 additions & 4 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
12+
ListNode node = new ListNode(value);
13+
node.next = top;
14+
top = node;
1315
}
1416

1517
public int pop() {
16-
return 0;
18+
if (isEmpty()) {
19+
throw new IllegalStateException("Stack is empty");
20+
}
21+
int valToPop = top.val;
22+
top = top.next;
23+
return valToPop;
1724
}
1825

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

2334
public boolean isEmpty() {
24-
return true;
35+
return top == null;
2536
}
2637
}

0 commit comments

Comments
 (0)