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