1
1
package com .codedifferently .lesson12 ;
2
2
3
-
4
3
public class Lesson12 {
5
4
6
5
@@ -9,21 +8,23 @@ public class Lesson12 {
9
8
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
10
9
*/
11
10
public String gameResult (ListNode head ) {
12
- int evenCounter = 0 ; //Keeps track of even score
13
- int oddCounter = 0 ; //Keeps track of odd score
14
-
11
+ int evenCounter = 0 ; //Keeps track of even score
12
+ int oddCounter = 0 ; //Keeps track of odd score
15
13
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
14
+ while (head != null && head .next != null ) { //Goes through the Linked List until theres no more pairs
15
+ int evenValue = head .val ; //Makes the first even value to compare equal to the first number of the list
16
+ int oddValue = head .next .val ; //Makes the first odd value to compare equal to the second number of the list
18
17
19
18
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
-
19
+ if ( evenValue > oddValue ) {
20
+ evenCounter = evenCounter + 1 ;
21
+ //Compares the even and odd value, adds 1 to even score if even is greater
22
+ } else {
23
+ oddCounter = oddCounter + 1 ;
24
+ } //Compares the even and odd value, adds 1 to odd score if odd is greater
25
+ // head.val = head.next.val; //Brings out the next pair befor restarting the loop
26
+ head = head .next ; //Brings out the next pair befor restarting the loop
27
+ }
27
28
28
29
if (evenCounter > oddCounter ) {
29
30
return "Even" ;
@@ -34,7 +35,7 @@ public String gameResult(ListNode head) {
34
35
} else {
35
36
return "Tie" ;
36
37
} //Compares the even and odd score, prints 'Tie' if the two scores are equal
37
-
38
+
38
39
}
39
40
40
41
}
0 commit comments