Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Adds, NileJackson's extra credit for Lesson12.ts #447

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion lesson_12/structs_ts/src/lesson12.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@ export class Lesson12 {
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
*/
public gameResult(head: ListNode | null): string {
return '';
if (!head) return 'Tie';

let current: ListNode | null = head;
const scores: number[] = [0, 0];

while (current && current.next) {
if (current.val !== current.next.val && current.val % 2 == 0) {
if (current.val > current.next.val) {
scores[0]++;
} else {
scores[1]++;
}
}
current = current.next;
}
if (scores[0] > scores[1]) {
return 'Even';
} else if (scores[0] < scores[1]) {
return 'Odd';
} else {
return 'Tie';
}
}
}
21 changes: 15 additions & 6 deletions lesson_12/structs_ts/src/stack.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { ListNode } from './list_node.js';

export class Stack {
Expand All @@ -7,19 +8,27 @@ export class Stack {
this.top = undefined;
}

push(value: number): void {
throw new Error('Not implemented');
push(val: number): void {
const newNode = new ListNode(val);
newNode.next = this.top;
this.top = newNode;
}

pop(): number | undefined {
throw new Error('Not implemented');
if (this.isEmpty()) return undefined;
const val = this.top!.val;
this.top = this.top!.next;
return val;
}

peek(): number | null {
throw new Error('Not implemented');
peek(): number {
if (this.isEmpty()) {
/* empty */
}
return this.top!.val;
}

isEmpty(): boolean {
throw new Error('Not implemented');
return this.top === undefined;
}
}