Skip to content

feat: Implemented Stack Class and generated logic for gameResult #417

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 3 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,44 @@
package com.codedifferently.lesson12;

public class Lesson12 {
// node1 node2 node3 node4 null
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't have this comment in the code


/**
* 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;
// These vars keep track of the point for the even and odd indicies.
int oddPoints = 0;
int evenPoints = 0;

/*
* This var will be redeclared throughout the loop and be used
* to point to the node pairs.
*/
ListNode curr = head;

/**
* This loop will do two comparisons. The 1st comparison is to determine which index has a
* greater value. The 2nd comparison checks which index team is more points
*/
while (curr != null) {
if (curr.val > curr.next.val) {
evenPoints++;
} else if (curr.val < curr.next.val) {
oddPoints++;
}

// "curr.next.next" allows for the loop to skip over an index
curr = curr.next.next;
}

if (oddPoints > evenPoints) {
return "Odd";
} else if (oddPoints < evenPoints) {
return "Even";
} else {
return "Tie";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,28 @@ public Stack() {

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

public int pop() {
return 0;
if (isEmpty()) {
throw new IllegalStateException("Empty");
}
int oldTopVal = top.val;
top = top.next;
return oldTopVal;
}

public int peek() {
return 0;
if (isEmpty()) {
throw new IllegalStateException("Empty");
}
return top.val;
}

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