File tree Expand file tree Collapse file tree 1 file changed +4
-12
lines changed
lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12 Expand file tree Collapse file tree 1 file changed +4
-12
lines changed Original file line number Diff line number Diff line change 2
2
3
3
public class Lesson12 {
4
4
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
5
public String gameResult (ListNode head ) {
10
- //create two variables to count points for "Odd" and "Even"
6
+
11
7
int oddPoints = 0 ;
12
8
int evenPoints = 0 ;
13
9
14
- // Loop through the linked list to get pairs (so two nodes at a time)
15
10
ListNode current = head ;
16
- // Check each pair of nodes (odd and even):
17
- // compare their values
18
- while (current != null && current .next != null ) {
19
- // add a point to "Even" if the odd-indexed node is higher and vice versa
11
+
12
+ while (current != null && current .next != null ) {
20
13
if (current .val > current .next .val ){
21
14
evenPoints ++;
22
15
} else if (current .val < current .next .val ){
23
16
oddPoints ++;
24
17
}
25
18
current = current .next .next ;
26
19
}
27
- // Decide the winner
28
- // compare the points and choose the winner or tie
20
+
29
21
if (oddPoints > evenPoints ) {
30
22
return "Odd" ;
31
23
} else if (evenPoints > oddPoints ){
You can’t perform that action at this time.
0 commit comments