Skip to content

Commit 3bc3669

Browse files
chore: implemented functions in the lesson12.ts and stack.ts files
1 parent 91bf9d5 commit 3bc3669

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
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+
let even = 0;
10+
let odd = 0;
11+
let evenNode: ListNode | null = head;
12+
let oddNode: ListNode | null = head?.next ?? null;
13+
while (evenNode != null && oddNode != null) {
14+
if (evenNode.val > oddNode.val) {
15+
even++;
16+
} else {
17+
odd++;
18+
}
19+
evenNode = evenNode.next?.next ?? null;
20+
oddNode = oddNode.next?.next ?? null;
21+
}
22+
if (even > odd) {
23+
return 'Even';
24+
} else if (odd > even) {
25+
return 'Odd';
26+
} else {
27+
return 'Tie';
28+
}
1029
}
1130
}

lesson_12/structs_ts/src/stack.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,24 @@ export class Stack {
88
}
99

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

1415
pop(): number | undefined {
15-
throw new Error('Not implemented');
16+
const value = this.top?.val;
17+
this.top = this.top?.next;
18+
return value;
1619
}
1720

1821
peek(): number | null {
19-
throw new Error('Not implemented');
22+
if (!this.top) {
23+
throw new Error('Stack is empty');
24+
}
25+
return this.top.val;
2026
}
2127

2228
isEmpty(): boolean {
23-
throw new Error('Not implemented');
29+
return this.top == null;
2430
}
2531
}

0 commit comments

Comments
 (0)