File tree Expand file tree Collapse file tree 4 files changed +40
-14
lines changed
structs_java/structs_app/src/main/java/com/codedifferently/lesson12 Expand file tree Collapse file tree 4 files changed +40
-14
lines changed Original file line number Diff line number Diff line change @@ -10,17 +10,15 @@ public String gameResult(ListNode head) {
10
10
int oddPoints = 0 ;
11
11
int evenPoints = 0 ;
12
12
13
- ListNode current = head ;
14
-
15
- while ( current != null && current . next != null ) {
13
+ for ( ListNode current = head ;
14
+ current != null && current . next != null ;
15
+ current = current . next . next ) {
16
16
if (current .val > current .next .val ) {
17
17
evenPoints ++;
18
18
} else {
19
19
oddPoints ++;
20
20
}
21
- current = current .next .next ;
22
21
}
23
-
24
22
if (oddPoints > evenPoints ) {
25
23
return "Odd" ;
26
24
} else if (evenPoints > oddPoints ) {
Original file line number Diff line number Diff line change 34
34
"tsc-watch" : " ^6.2.0" ,
35
35
"typescript" : " ^5.6.2" ,
36
36
"typescript-eslint" : " ^8.7.0"
37
- },
38
- "dependencies" : {}
37
+ }
39
38
}
Original file line number Diff line number Diff line change @@ -6,6 +6,25 @@ 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 oddPoints = 0 ;
10
+ let evenPoints = 0 ;
11
+ let current : ListNode | null = head ;
12
+
13
+ while ( current && current . next ) {
14
+ if ( current . val > current . next . val ) {
15
+ evenPoints ++ ;
16
+ } else {
17
+ oddPoints ++ ;
18
+ }
19
+ current = current . next . next || null ;
20
+ }
21
+
22
+ if ( oddPoints > evenPoints ) {
23
+ return "Odd" ;
24
+ } else if ( evenPoints > oddPoints ) {
25
+ return "Even" ;
26
+ } else {
27
+ return "Tie" ;
28
+ }
10
29
}
11
30
}
Original file line number Diff line number Diff line change 1
1
import { ListNode } from './list_node.js' ;
2
2
3
3
export class Stack {
4
- private top : ListNode | undefined ;
4
+ private top : ListNode | null ;
5
5
6
6
constructor ( ) {
7
- this . top = undefined ;
7
+ this . top = null ;
8
8
}
9
9
10
10
push ( value : number ) : void {
11
- throw new Error ( 'Not implemented' ) ;
11
+ const newNode = new ListNode ( value ) ;
12
+ newNode . next = this . top !== null ? this . top : undefined ;
13
+ this . top = newNode ;
12
14
}
13
15
14
16
pop ( ) : number | undefined {
15
- throw new Error ( 'Not implemented' ) ;
17
+ if ( this . isEmpty ( ) ) {
18
+ throw new Error ( 'Stack is empty' ) ;
19
+ }
20
+ const value = this . top ! . val ;
21
+ this . top = this . top ! . next || null ;
22
+ return value ;
16
23
}
17
24
18
25
peek ( ) : number | null {
19
- throw new Error ( 'Not implemented' ) ;
26
+ if ( this . isEmpty ( ) ) {
27
+ throw new Error ( 'Stack is empty' ) ;
28
+ }
29
+ return this . top ! . val ;
20
30
}
21
31
22
32
isEmpty ( ) : boolean {
23
- throw new Error ( 'Not implemented' ) ;
33
+ return this . top === null ;
24
34
}
25
35
}
You can’t perform that action at this time.
0 commit comments