File tree Expand file tree Collapse file tree 2 files changed +44
-4
lines changed
lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12 Expand file tree Collapse file tree 2 files changed +44
-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,25 @@ public Stack() {
10
10
11
11
public void push (int value ) {
12
12
// Your code here
13
+ ListNode newTop = new ListNode (value );
14
+ if (top == null ) {
15
+ top = newTop ;
16
+ } else {
17
+ newTop .next = top ;
18
+ }
13
19
}
14
20
15
21
public int pop () {
16
- return 0 ;
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
+ return top . val ;
21
29
}
22
30
23
31
public boolean isEmpty () {
24
- return true ;
32
+ return top == null ;
25
33
}
26
34
}
You can’t perform that action at this time.
0 commit comments