Skip to content

Commit 9c96edc

Browse files
committed
feat: adds Kimberlee's ListNode.java file
1 parent ad543f8 commit 9c96edc

File tree

1 file changed

+35
-0
lines changed
  • lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12

1 file changed

+35
-0
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,39 @@ public class ListNode {
1414
this.val = val;
1515
this.next = next;
1616
}
17+
18+
19+
public static String listWinner(ListNode head) {
20+
int evenScore = 0;
21+
int oddScore = 0;
22+
23+
ListNode current = head;
24+
while (current != null && current.next != null) {
25+
int evenVal = current.val;
26+
int oddVal = current.next.val;
27+
28+
if (evenVal > oddVal) {
29+
evenScore++;
30+
} else if (oddVal > evenVal) {
31+
oddScore++;
32+
}
33+
34+
current = current.next.next;
35+
}
36+
37+
if (evenScore > oddScore) {
38+
return "Even";
39+
} else if (oddScore > evenScore) {
40+
return "Odd";
41+
} else {
42+
return "Tie";
43+
}
44+
}
45+
46+
public static void main(String[] args) {
47+
ListNode head = new ListNode(2, new ListNode(5, new ListNode(4, new ListNode(7, new ListNode(20, new ListNode(5))))));
48+
49+
String winner = listWinner(head);
50+
System.out.println("The winner is: " + winner);
1751
}
52+
}

0 commit comments

Comments
 (0)