File tree Expand file tree Collapse file tree 2 files changed +47
-4
lines changed
lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12 Expand file tree Collapse file tree 2 files changed +47
-4
lines changed Original file line number Diff line number Diff line change 1
1
package com .codedifferently .lesson12 ;
2
2
3
3
public class Lesson12 {
4
+ // node1 node2 node3 node4 null
4
5
5
6
/**
6
7
* Provide the solution to LeetCode 3062 here:
7
8
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
8
9
*/
9
10
public String gameResult (ListNode head ) {
10
- return null ;
11
+ // These vars keep track of the point for the even and odd indicies.
12
+ int oddPoints = 0 ;
13
+ int evenPoints = 0 ;
14
+
15
+ /*
16
+ * This var will be redeclared throughout the loop and be used
17
+ * to point to the node pairs.
18
+ */
19
+ ListNode curr = head ;
20
+
21
+ /**
22
+ * This loop will do two comparisons. The 1st comparison is to determine which index has a
23
+ * greater value. The 2nd comparison checks which index team is more points
24
+ */
25
+ while (curr != null ) {
26
+ if (curr .val > curr .next .val ) {
27
+ evenPoints ++;
28
+ } else if (curr .val < curr .next .val ) {
29
+ oddPoints ++;
30
+ }
31
+
32
+ // "curr.next.next" allows for the loop to skip over an index
33
+ curr = curr .next .next ;
34
+ }
35
+
36
+ if (oddPoints > evenPoints ) {
37
+ return "Odd" ;
38
+ } else if (oddPoints < evenPoints ) {
39
+ return "Even" ;
40
+ } else {
41
+ return "Tie" ;
42
+ }
11
43
}
12
44
}
Original file line number Diff line number Diff line change @@ -10,17 +10,28 @@ public Stack() {
10
10
11
11
public void push (int value ) {
12
12
// Your code here
13
+ ListNode newTop = new ListNode (value );
14
+ newTop .next = top ;
15
+ top = newTop ;
13
16
}
14
17
15
18
public int pop () {
16
- return 0 ;
19
+ if (isEmpty ()) {
20
+ throw new IllegalStateException ("Empty" );
21
+ }
22
+ int oldTopVal = top .val ;
23
+ top = top .next ;
24
+ return oldTopVal ;
17
25
}
18
26
19
27
public int peek () {
20
- return 0 ;
28
+ if (isEmpty ()) {
29
+ throw new IllegalStateException ("Empty" );
30
+ }
31
+ return top .val ;
21
32
}
22
33
23
34
public boolean isEmpty () {
24
- return true ;
35
+ return top == null ;
25
36
}
26
37
}
You can’t perform that action at this time.
0 commit comments