File tree Expand file tree Collapse file tree 1 file changed +34
-6
lines changed
lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12 Expand file tree Collapse file tree 1 file changed +34
-6
lines changed Original file line number Diff line number Diff line change 1
1
package com .codedifferently .lesson12 ;
2
2
3
+
3
4
public class Lesson12 {
4
5
5
- /**
6
- * Provide the solution to LeetCode 3062 here:
7
- * https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
8
- */
9
- public String gameResult (ListNode head ) {
10
- return null ;
6
+
7
+ /**
8
+ * Provide the solution to LeetCode 3062 here:
9
+ * https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
10
+ */
11
+ public String gameResult (ListNode head ) {
12
+ int evenCounter = 0 ; //Keeps track of even score
13
+ int oddCounter = 0 ; //Keeps track of odd score
14
+
15
+
16
+ int evenValue = head .val ; //Makes the first even value to compare equal to the first number of the list
17
+ int oddValue = head .next .val ; //Makes the first odd value to compare equal to the second number of the list
18
+
19
+
20
+ if ( evenValue > oddValue ) {
21
+ evenCounter = evenCounter + 1 ;
22
+ //Compares the even and odd value, adds 1 to even score if even is greater
23
+ } else {
24
+ oddCounter = ++oddCounter + 1 ;
25
+ } //Compares the even and odd value, adds 1 to odd score if odd is greater
26
+
27
+
28
+ if (evenCounter > oddCounter ) {
29
+ return "Even" ;
30
+ //Compares the even and odd score, prints 'Even' if Evens score is greater
31
+ } if (evenCounter < oddCounter ) {
32
+ return "Odd" ;
33
+ //Compares the even and odd score, prints 'Odd' if odds score is greater
34
+ } else {
35
+ return "Tie" ;
36
+ } //Compares the even and odd score, prints 'Tie' if the two scores are equal
37
+
11
38
}
39
+
12
40
}
You can’t perform that action at this time.
0 commit comments