Skip to content

Commit 567346d

Browse files
committed
Feat: Adds Shawn Dunsmore Jr java/stack.
1 parent 8676acd commit 567346d

File tree

2 files changed

+53
-25
lines changed

2 files changed

+53
-25
lines changed

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,29 @@
22

33
public class Lesson12 {
44

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

33
/** Implement the below Stack by providing code for the class methods. */
4-
public class Stack {
5-
private ListNode top;
4+
public class Stack {
5+
private ListNode top;
66

7-
public Stack() {
8-
this.top = null;
9-
}
7+
public Stack() {
8+
this.top = null;
9+
}
1010

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

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

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

23-
public boolean isEmpty() {
24-
return true;
25-
}
33+
public boolean isEmpty() {
34+
return top == null;
35+
}
2636
}

0 commit comments

Comments
 (0)