Skip to content

Commit 7450dd4

Browse files
committed
feat: implements gameResult method to determine winner based on linked list values
1 parent ac458a6 commit 7450dd4

File tree

1 file changed

+27
-3
lines changed
  • lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12

1 file changed

+27
-3
lines changed

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,30 @@ public class Lesson12 {
77
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
88
*/
99
public String gameResult(ListNode head) {
10-
return null;
11-
}
12-
}
10+
if (head == null || head.next == null) {
11+
return "No Pairs";
12+
}
13+
14+
int oddPoints = 0, evenPoints = 0;
15+
ListNode current = head;
16+
17+
while (current != null && current.next != null) {
18+
if (current.val < current.next.val) {
19+
oddPoints++;
20+
} else {
21+
evenPoints++;
22+
}
23+
current = current.next.next;
24+
}
25+
26+
27+
if (oddPoints > evenPoints) {
28+
return "Odd";
29+
} else if (evenPoints > oddPoints) {
30+
return "Even";
31+
} else {
32+
return "Tie";
33+
}
34+
}
35+
36+
}

0 commit comments

Comments
 (0)