Skip to content

Commit c3e1b5d

Browse files
committed
feat:add Davis stack and leetcode prompt (3062)HW
1 parent 070b57b commit c3e1b5d

File tree

1 file changed

+21
-1
lines changed
  • lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12

1 file changed

+21
-1
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ 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;
10+
int[] ls = new int[2];
11+
while (head != null && head.next != null) {
12+
// Iterate through the linked list until the second last node
13+
if (head.val != head.next.val && head.val % 2 == 0) {
14+
// Check if we have different values and if the current value is even
15+
if (head.val > head.next.val) {
16+
ls[head.val % 2] += 1;
17+
} else {
18+
ls[head.next.val % 2] += 1;
19+
}
20+
}
21+
head = head.next;
22+
}
23+
24+
if (ls[0] > ls[1]) {
25+
return "Even";
26+
} else if (ls[0] == ls[1]) {
27+
return "Tie";
28+
} else {
29+
return "Odd";
30+
}
1131
}
1232
}

0 commit comments

Comments
 (0)