Skip to content

Feat: edited methods for Lesson12.java and Stack.java - James Capparell #405

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
Expand Up @@ -2,11 +2,59 @@

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 score_even = 0;
int score_odd = 0;
int evenNum = 0;
int oddNum = 0;
for (int i = 0; i < getLength(head); i++) {
if (i % 2 == 0) {
ListNode resultNode = getElementAt(head, i);
evenNum = resultNode.val;
} else if (i % 2 == 1) {
ListNode resultNode = getElementAt(head, i);
oddNum = resultNode.val;
if (evenNum > oddNum) {
score_even += 1;
} else if (evenNum < oddNum) {
score_odd += 1;
}
}
}
if (score_even > score_odd) {
return "Even";
} else if (score_even < score_odd) {
return "Odd";
} else {
return "Tie";
}
}

public ListNode getElementAt(ListNode head, int position) {
ListNode current = head;
int index = 0;

// Traverse the list until reaching the desired position
while (current != null) {
if (index == position) {
return current; // Return the node at the specified position
}
current = current.next; // Move to the next node
index++; // Increment the index
}

return null; // Return null if the position is out of bounds
}

public int getLength(ListNode head) {
int length = 0;
ListNode current = head;

while (current != null) {
length++; // increment to keep count through each element
current = current.next; // moves on to next element
}

return length;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,27 @@ public Stack() {
}

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

public int pop() {
return 0;
if (isEmpty()) {
throw new IllegalStateException("Stack is empty");
}
int value = top.val;
top = top.next;
return value;
}

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

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