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