File tree Expand file tree Collapse file tree 2 files changed +54
-5
lines changed Expand file tree Collapse file tree 2 files changed +54
-5
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,32 @@ 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 e_pts = 0 ;
10
+ let o_pts = 0 ;
11
+
12
+ if ( head === null ) {
13
+ return "" ;
14
+ }
15
+
16
+ let curr : ListNode | undefined = head ;
17
+
18
+ while ( curr != null ) {
19
+ let next : ListNode | undefined = curr . next ;
20
+ if ( next === undefined ) {
21
+ return "" ;
22
+ }
23
+ if ( curr . val > next ! . val ) {
24
+ e_pts ++ ;
25
+ } else if ( curr . val < next ! . val ) {
26
+ o_pts ++ ;
27
+ }
28
+ curr = next . next ;
29
+ }
30
+
31
+ if ( e_pts === o_pts ) {
32
+ return "Tie" ;
33
+ }
34
+
35
+ return e_pts > o_pts ? "Even" : "Odd" ;
10
36
}
11
37
}
Original file line number Diff line number Diff line change @@ -8,18 +8,41 @@ export class Stack {
8
8
}
9
9
10
10
push ( value : number ) : void {
11
- throw new Error ( 'Not implemented' ) ;
11
+ let node : ListNode | undefined = new ListNode ( value ) ;
12
+
13
+ node . next = this . top ;
14
+ this . top = node ;
15
+
12
16
}
13
17
14
18
pop ( ) : number | undefined {
15
- throw new Error ( 'Not implemented' ) ;
19
+ if ( this . isEmpty ( ) ) {
20
+ throw new Error ( 'Stack is empty' ) ;
21
+ }
22
+
23
+ if ( this . top ) {
24
+ let value_to_pop : number = this . top . val
25
+ this . top = this . top . next ;
26
+ return value_to_pop ;
27
+ }
28
+
29
+ return undefined ;
16
30
}
17
31
18
32
peek ( ) : number | null {
19
- throw new Error ( 'Not implemented' ) ;
33
+ if ( this . isEmpty ( ) ) {
34
+ throw new Error ( 'Stack is empty' ) ;
35
+ }
36
+
37
+ if ( this . top ) {
38
+ return this . top . val ;
39
+ }
40
+
41
+ return null ;
42
+
20
43
}
21
44
22
45
isEmpty ( ) : boolean {
23
- throw new Error ( 'Not implemented' ) ;
46
+ return top === undefined ;
24
47
}
25
48
}
You can’t perform that action at this time.
0 commit comments