Skip to content

Commit eedad00

Browse files
author
AmiyahJo
committed
feat: adds code to solve the problem
1 parent 1f4bdbd commit eedad00

File tree

1 file changed

+18
-17
lines changed
  • lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12

1 file changed

+18
-17
lines changed

lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
11
package com.codedifferently.lesson12;
22

3-
import java.util.Queue;
4-
53
public class Lesson12 {
64

75
/**
86
* Provide the solution to LeetCode 3062 here:
97
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
108
*/
119
public String gameResult(ListNode head) {
12-
//make a linked list
13-
Queue<Integer> queue = new LinkedList<Integer>();
14-
1510
//create two variables to count points for "Odd" and "Even"
16-
int Odd = 0;
17-
int Even = 0;
18-
// Loop through the linked list to get pairs (so two nodes at a time)
11+
int oddPoints = 0;
12+
int evenPoints = 0;
1913

14+
// Loop through the linked list to get pairs (so two nodes at a time)
15+
ListNode current = head;
2016
// Check each pair of nodes (odd and even):
2117
// compare their values
22-
23-
// add a point to "Odd" if the odd-indexed node is higher
24-
25-
//add a point to "Even" if the even-indexed node is higher
26-
18+
while (current != null && current.next != null) {
19+
// add a point to "Even" if the odd-indexed node is higher
20+
if (current.val < current.next.val){
21+
evenPoints++;
22+
}
23+
//add a point to "Odd" if the even-indexed node is higher
24+
else if (current.val > current.next.val){
25+
oddPoints++;
26+
}
27+
28+
current = current.next.next;
29+
}
2730
// Decide the winner
2831
// compare the points and choose the winner or tie
29-
if (Odd > Even) {
32+
if (oddPoints > evenPoints) {
3033
return("Team odd wins!");
31-
} else if (Even > Odd){
34+
} else if (evenPoints > oddPoints){
3235
return("Team Even wins");
3336
} else {
3437
return ("It's a tie");
@@ -40,5 +43,3 @@ public String gameResult(ListNode head) {
4043
// Constraints:
4144
// The number of nodes in the list is in the range [2, 100].
4245
// The number of nodes in the list is even. 1 <= Node.val <= 100
43-
// The value of each odd-indexed node is odd.
44-
// The value of each even-indexed node is even.

0 commit comments

Comments
 (0)