Skip to content

Commit 328ee9a

Browse files
committed
lesson_12_structs_ts
1 parent 413d6e0 commit 328ee9a

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

lesson_12/structs_ts/src/lesson12.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@ export class Lesson12 {
66
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
77
*/
88
public gameResult(head: ListNode | null): string {
9-
return '';
9+
const scores = [0, 0];
10+
11+
while (head?.next != null) {
12+
if(head?.val != head?.next?.val && head?.val % 2 == 0) {
13+
if(head?.val > head?.next.val) {
14+
scores[head?.val % 2] += 1;
15+
} else {
16+
scores[head?.next.val % 2] += 1;
17+
}
18+
}
19+
head = head?.next;
20+
}
21+
22+
if (scores[0] === scores[1]) {
23+
return 'Tie';
24+
} else if (scores[0] > scores[1]) {
25+
return 'Even';
26+
} else {
27+
return 'Odd';
28+
}
1029
}
1130
}

lesson_12/structs_ts/src/stack.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
11
import { ListNode } from './list_node.js';
22

33
export class Stack {
4-
private top: ListNode | undefined;
4+
private top: ListNode | null;
55

66
constructor() {
7-
this.top = undefined;
7+
this.top = null;
88
}
99

1010
push(value: number): void {
11-
throw new Error('Not implemented');
11+
const newNode = new ListNode(value, this.top ?? undefined);
12+
this.top = newNode;
1213
}
1314

14-
pop(): number | undefined {
15-
throw new Error('Not implemented');
15+
pop(): number {
16+
if (this.isEmpty()) {
17+
throw new Error('Stack Underflow: Cannot pop from an empty stack');
18+
}
19+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
20+
const poppedValue = this.top!.val;
21+
this.top = this.top?.next ?? null;
22+
return poppedValue;
1623
}
1724

18-
peek(): number | null {
19-
throw new Error('Not implemented');
25+
peek(): number {
26+
if (this.isEmpty()) {
27+
throw new Error('Stack is empty: Cannot peek');
28+
}
29+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
30+
return this.top!.val;
2031
}
2132

2233
isEmpty(): boolean {
23-
throw new Error('Not implemented');
34+
return this.top === null;
2435
}
2536
}

0 commit comments

Comments
 (0)