Skip to content

Commit 6b1b0f6

Browse files
committed
chore: added a missing semicolon
1 parent 811220e commit 6b1b0f6

File tree

1 file changed

+85
-2
lines changed
  • lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12

1 file changed

+85
-2
lines changed
Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,95 @@
11
package com.codedifferently.lesson12;
22

3+
import java.util.LinkedList;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
37
public class Lesson12 {
48

59
/**
610
* Provide the solution to LeetCode 3062 here:
711
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
812
*/
9-
public String gameResult(ListNode head) {
10-
return null;
13+
class TeamNode {
14+
int data;
15+
TeamNode next;
16+
17+
public TeamNode(int data) {
18+
this.data = data;
19+
this.next = null;
20+
}
21+
}
22+
23+
class LinkedList {
24+
TeamNode head;
25+
26+
public void add(int data) {
27+
TeamNode newTeamNode = new TeamNode(data);
28+
if (head == null) {
29+
head = newTeamNode;
30+
} else {
31+
TeamNode temp = head;
32+
while (temp.next != null) {
33+
temp = temp.next;
34+
}
35+
temp.next = newTeamNode;
36+
}
37+
}
38+
39+
public String gameResult(TeamNode head) {
40+
List<String> result = new ArrayList<>();
41+
int evenCounter = 0;
42+
int oddCounter = 0;
43+
TeamNode current = head;
44+
45+
while (current != null && current.next != null) {
46+
int evenValue = current.data;
47+
int oddValue = current.next.data;
48+
49+
if (evenValue > oddValue) {
50+
result.add("even");
51+
evenCounter++;
52+
} else {
53+
result.add("odd");
54+
oddCounter++;
55+
}
56+
57+
current = current.next.next;
58+
}
59+
60+
String winningTeam;
61+
if (evenCounter > oddCounter) {
62+
winningTeam = "even";
63+
} else if (oddCounter > evenCounter) {
64+
winningTeam = "odd";
65+
} else {
66+
winningTeam = "tie";
67+
}
68+
69+
System.out.println("Nodes with higher values in each pair: " + result);
70+
System.out.println("The team with the most high-values is: " + winningTeam);
71+
72+
return winningTeam;
73+
}
74+
}
75+
76+
77+
public class Main {
78+
public static void main(String[] args) {
79+
LinkedList list = new LinkedList();
80+
81+
// Adding some nodes to the linked list
82+
list.add(2);
83+
list.add(5);
84+
list.add(4);
85+
list.add(7);
86+
list.add(20);
87+
list.add(5);
88+
89+
// Call the function to compare pairs and determine the group with the most high-value pairs
90+
String winningTeam = list.gameResult(list.head);
91+
92+
// Output the dominant group
93+
System.out.println("Team with most high-value pairs: " + winningTeam);
1194
}
1295
}

0 commit comments

Comments
 (0)