|
1 | 1 | package com.codedifferently.lesson12; |
2 | 2 |
|
3 | 3 | public class Lesson12 { |
4 | | - |
5 | | - // public static Node { |
6 | | - // public int data; |
7 | | - // public Node next; |
8 | | - |
9 | | - // public Node(int data) { |
10 | | - // this.data = data; |
11 | | - // } |
12 | | - // } |
13 | 4 | /** |
14 | 5 | * Provide the solution to LeetCode 3062 here: |
15 | 6 | * https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game |
16 | 7 | */ |
17 | 8 | public static String gameResult(ListNode head) { |
18 | | - int[] scoreBoard = {0, 0}; |
| 9 | + int[] scoreBoard = {0, 0}; //Creates an array that will act as our scoreboard...hence the name |
19 | 10 |
|
20 | | - while (head.next != null) { |
21 | | - if (head.val != head.next.val && head.val % 2 == 0) { |
22 | | - if (head.val > head.next.val) { |
23 | | - scoreBoard[head.val % 2] += 1; |
| 11 | + while (head.next != null) { //Creates a loop that runs until the head Linked list is empty || null |
| 12 | + if (head.val != head.next.val && head.val % 2 == 0) { //Checks if the current node's value is not equal to the node next to it. |
| 13 | + //Also will check if the current not's value is even ( % 2 dividing by 2 to detemine) |
| 14 | + if (head.val > head.next.val) {//Updates the array based on index. |
| 15 | + scoreBoard[head.val % 2] += 1; //If the head is divisible by 2 then update the first Even teams point |
24 | 16 | } else { |
25 | | - scoreBoard[head.next.val % 2] += 1; |
| 17 | + scoreBoard[head.next.val % 2] += 1; |
26 | 18 | } |
27 | 19 | } |
28 | 20 | head = head.next; // Moves to the next Node |
29 | 21 | } |
30 | 22 |
|
31 | 23 | if (scoreBoard[0] == scoreBoard[1]) { |
32 | | - return "Tie"; |
| 24 | + return "Tie"; //Returns tie if the scoreboard is even |
33 | 25 | } else if (scoreBoard[0] > scoreBoard[1]) { |
34 | | - return "Even"; |
| 26 | + return "Even"; //if the Even side [0] is greater than 1 return "Even" |
35 | 27 | } else { |
36 | | - return "Odd"; |
| 28 | + return "Odd"; // Else return "Odd" |
37 | 29 | } |
38 | 30 | } |
39 | 31 | } |
40 | | - |
41 | | - // public static Node createLinkedList(int[] arr) { |
42 | | - // if (arr.length == 0) return null; |
43 | | - |
44 | | - // Node head = new Node(arr[0]); //Creates the first node and names it head |
45 | | - // Node current = head; //Tracks the current head inside the loop |
46 | | - |
47 | | - // for(int i = 1; i < arr.length; ++i) { //Starts a for loop that is at the second element |
48 | | - // current.next = new Node(arr[i]); //creates a new node and links it |
49 | | - // current = current.next; |
50 | | - // } |
51 | | - // return head; |
52 | | - |
53 | | - // } |
54 | | - |
55 | | - // public static void main(String[] args) { |
56 | | - // int[] arr = {2, 5, 4, 7, 20, 5}; |
57 | | - |
58 | | - // System.out.println(createLinkedList(arr)); |
59 | | - // } |
60 | | -// } |
0 commit comments