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