Skip to content

Commit 50d10be

Browse files
committed
feat: Adding lesson12 implementation of leetcode with stack by Yemi
1 parent 439780a commit 50d10be

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.codedifferently.lesson12;
22

33
public class Lesson12 {
4-
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-
*/
94
public String gameResult(ListNode head) {
10-
return null;
5+
int result[] = new int[] {0, 0};
6+
while (head.next != null) {
7+
if (head.val != head.next.val && head.val % 2 == 0) {
8+
if (head.val > head.next.val) result[head.val % 2] += 1;
9+
else result[head.next.val % 2] += 1;
10+
}
11+
head = head.next;
12+
}
13+
return (result[0] == result[1]) ? "Tie" : (result[0] > result[1] ? "Even" : "Odd");
1114
}
1215
}
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.codedifferently.lesson12;
22

3-
/** Implement the below Stack by providing code for the class methods. */
43
public class Stack {
54
private ListNode top;
65

@@ -9,18 +8,30 @@ public Stack() {
98
}
109

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

1516
public int pop() {
16-
return 0;
17+
int topmostValue = 0;
18+
if (isEmpty()) {
19+
return Integer.parseInt(null);
20+
} else {
21+
topmostValue = top.val;
22+
top = top.next;
23+
return topmostValue;
24+
}
1725
}
1826

1927
public int peek() {
20-
return 0;
28+
if (isEmpty()) {
29+
return Integer.parseInt(null);
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)