Skip to content
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

feat: adds Kimberlee's Lesson 12: Data Structures: Stacks, Queue, Linked Lists #438

Closed
wants to merge 12 commits into from
Closed
Original file line number Diff line number Diff line change
@@ -1,12 +1,52 @@
package com.codedifferently.lesson12;


import java.util.ArrayList;
import java.util.List;

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;


public String gameResult(ListNode head) {
List<String> result = new ArrayList<>();
int evenCounter = 0;
int oddCounter = 0;
ListNode current = head;

while (current != null && current.next != null) {
int evenValue = current.data;
int oddValue = current.next.data;

if (evenValue > oddValue) {
result.add("even");
evenCounter++;
} else {
result.add("odd");
oddCounter++;
}

current = current.next.next;
}

String winningTeam;
if (evenCounter > oddCounter) {
winningTeam = "even";
} else if (oddCounter > evenCounter) {
winningTeam = "odd";
} else {
winningTeam = "tie";
}

System.out.println("Node team with higher value in each pair: " + result);
System.out.println("The team with the most high-values is: " + winningTeam);

return winningTeam;
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ public class ListNode {
this.val = val;
this.next = next;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,29 @@ public Stack() {

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

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

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

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

Loading