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,25 @@ 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 e_pts = 0 ;
11
+ int o_pts = 0 ;
12
+
13
+ ListNode curr = head ;
14
+
15
+ while (curr != null ) {
16
+ ListNode next = curr .next ;
17
+ if (curr .val > next .val ) {
18
+ e_pts ++;
19
+ } else if (curr .val < next .val ) {
20
+ o_pts ++;
21
+ }
22
+ curr = next .next ;
23
+ }
24
+
25
+ if (e_pts == o_pts ) {
26
+ return "Tie" ;
27
+ }
28
+
29
+ return e_pts > o_pts ? "Even" : "Odd" ;
11
30
}
12
31
}
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
12
+ ListNode node = new ListNode (value );
13
+ node .next = top ;
14
+ top = node ;
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 valToPop = top .val ;
22
+ top = top .next ;
23
+ return valToPop ;
17
24
}
18
25
19
26
public int peek () {
20
- return 0 ;
27
+ if (isEmpty ()) {
28
+ throw new IllegalStateException ("Stack is empty" );
29
+ }
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