File tree Expand file tree Collapse file tree 4 files changed +50
-18
lines changed
lesson_12/structs_java/structs_app/src
main/java/com/codedifferently/lesson12
test/java/com/codedifferently/lesson12 Expand file tree Collapse file tree 4 files changed +50
-18
lines changed Original file line number Diff line number Diff line change @@ -6,7 +6,30 @@ public class Lesson12 {
6
6
* Provide the solution to LeetCode 3062 here:
7
7
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
8
8
*/
9
- public String gameResult (ListNode head ) {
10
- return null ;
11
- }
12
- }
9
+ public String gameResult (ListNode head ) {
10
+ int oddPoints = 0 ;
11
+ int evenPoints = 0 ;
12
+ ListNode current = head ;
13
+
14
+ while (current != null && current .next != null ) {
15
+ int evenValue = current .val ;
16
+ int oddValue = current .next .val ;
17
+
18
+ if (evenValue > oddValue ) {
19
+ evenPoints ++;
20
+ } else if (oddValue > evenValue ) {
21
+ oddPoints ++;
22
+ }
23
+
24
+ current = current .next .next ;
25
+ }
26
+
27
+ if (evenPoints > oddPoints ) {
28
+ return "Even" ;
29
+ } else if (oddPoints > evenPoints ) {
30
+ return "Odd" ;
31
+ } else {
32
+ return "Tie" ;
33
+ }
34
+ }
35
+ }
Original file line number Diff line number Diff line change @@ -9,18 +9,29 @@ public Stack() {
9
9
}
10
10
11
11
public void push (int value ) {
12
- // Your code here
13
- }
12
+ ListNode node = new ListNode (value );
13
+ node .next = top ;
14
+ top = node ;
15
+ }
14
16
15
- public int pop () {
16
- return 0 ;
17
- }
17
+ public int pop () {
18
+ if (isEmpty ()){
19
+ throw new IllegalStateException ("Stack has no values" );
20
+ }
21
+ int valToPop = top .val ;
22
+ top = top .next ;
23
+ return valToPop ;
24
+ }
18
25
19
- public int peek () {
20
- return 0 ;
21
- }
26
+ public int peek () {
27
+ if (isEmpty ()){
28
+ throw new IllegalStateException ("Stack has no values" );
29
+ }
22
30
23
- public boolean isEmpty () {
24
- return true ;
25
- }
26
- }
31
+ return top .val ;
32
+ }
33
+
34
+ public boolean isEmpty () {
35
+ return top == null ;
36
+ }
37
+ }
Original file line number Diff line number Diff line change 1
1
package com .codedifferently .lesson12 ;
2
2
3
3
import static org .junit .jupiter .api .Assertions .assertEquals ;
4
-
5
4
import org .junit .jupiter .api .Test ;
6
5
7
6
class Lesson12Test {
Original file line number Diff line number Diff line change 4
4
import static org .junit .jupiter .api .Assertions .assertFalse ;
5
5
import static org .junit .jupiter .api .Assertions .assertThrows ;
6
6
import static org .junit .jupiter .api .Assertions .assertTrue ;
7
-
8
7
import org .junit .jupiter .api .Test ;
9
8
10
9
class StackTest {
You can’t perform that action at this time.
0 commit comments