File tree Expand file tree Collapse file tree 2 files changed +35
-5
lines changed
lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12 Expand file tree Collapse file tree 2 files changed +35
-5
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,26 @@ public class Lesson12 {
7
7
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
8
8
*/
9
9
public String gameResult (ListNode head ) {
10
- return null ;
10
+ int oddPoints = 0 ;
11
+ int evenPoints = 0 ;
12
+
13
+ ListNode current = head ;
14
+
15
+ while (current != null && current .next != null ) {
16
+ if (current .val > current .next .val ) {
17
+ evenPoints ++;
18
+ } else {
19
+ oddPoints ++;
20
+ }
21
+ current = current .next .next ;
22
+ }
23
+
24
+ if (oddPoints > evenPoints ) {
25
+ return "Odd" ;
26
+ } else if (evenPoints > oddPoints ) {
27
+ return "Even" ;
28
+ } else {
29
+ return "Tie" ;
30
+ }
11
31
}
12
32
}
Original file line number Diff line number Diff line change @@ -9,18 +9,28 @@ public Stack() {
9
9
}
10
10
11
11
public void push (int value ) {
12
- // Your code here
12
+ ListNode newNode = new ListNode (value );
13
+ newNode .next = top ;
14
+ top = newNode ;
13
15
}
14
16
15
17
public int pop () {
16
- return 0 ;
18
+ if (isEmpty ()) {
19
+ throw new IllegalStateException ("Stack is empty" );
20
+ }
21
+ int value = top .val ;
22
+ top = top .next ;
23
+ return value ;
17
24
}
18
25
19
26
public int peek () {
20
- return 0 ;
27
+ if (isEmpty ()) {
28
+ throw new IllegalStateException ("Stack is empty" );
29
+ }
30
+ return top .val ;
21
31
}
22
32
23
33
public boolean isEmpty () {
24
- return true ;
34
+ return top == null ;
25
35
}
26
36
}
You can’t perform that action at this time.
0 commit comments