Skip to content

feat: Adding lesson12 implementation of leetcode with stack by Yemi #397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.codedifferently.lesson12;

public class Lesson12 {

/**
* Provide the solution to LeetCode 3062 here:
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
*/
public String gameResult(ListNode head) {
return null;
int result[] = new int[] {0, 0};
while (head.next != null) {
if (head.val != head.next.val && head.val % 2 == 0) {
if (head.val > head.next.val) result[head.val % 2] += 1;
else result[head.next.val % 2] += 1;
}
head = head.next;
}
return (result[0] == result[1]) ? "Tie" : (result[0] > result[1] ? "Even" : "Odd");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.codedifferently.lesson12;

/** Implement the below Stack by providing code for the class methods. */
public class Stack {
private ListNode top;

Expand All @@ -9,18 +8,30 @@ public Stack() {
}

public void push(int value) {
// Your code here
ListNode newNode = new ListNode(value);
newNode.next = top;
top = newNode;
}

public int pop() {
return 0;
int topmostValue = 0;
if (isEmpty()) {
return Integer.parseInt(null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should throw an exception here since it's impossible to pop an element off of an empty stack.

} else {
topmostValue = top.val;
top = top.next;
return topmostValue;
}
}

public int peek() {
return 0;
if (isEmpty()) {
return Integer.parseInt(null);
}
return top.val;
}

public boolean isEmpty() {
return true;
return top == null;
}
}