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