Skip to content

feat: added Stack impl/ LeetCode 3062 finished in TypeScript - Xavier Cruz #395

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

Closed
Show file tree
Hide file tree
Changes from 2 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
28 changes: 27 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,32 @@ 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 '';
let e_pts = 0;
let o_pts = 0;

if (head === null) {
return '';
}

let curr: ListNode | undefined = head;

while (curr != null) {
const next: ListNode | undefined = curr.next;
if (next === undefined) {
return '';
}
if (curr.val > next?.val) {
e_pts++;
} else if (curr.val < next?.val) {
o_pts++;
}
curr = next.next;
}

if (e_pts === o_pts) {
return 'Tie';
}

return e_pts > o_pts ? 'Even' : 'Odd';
}
}
29 changes: 25 additions & 4 deletions lesson_12/structs_ts/src/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,39 @@ export class Stack {
}

push(value: number): void {
throw new Error('Not implemented');
const node: ListNode | undefined = new ListNode(value);

node.next = this.top;
this.top = node;
}

pop(): number | undefined {
throw new Error('Not implemented');
if (this.isEmpty()) {
throw new Error('Stack is empty');
}

if (this.top) {
const value_to_pop: number = this.top.val;
this.top = this.top.next;
return value_to_pop;
}

return undefined;
}

peek(): number | null {
throw new Error('Not implemented');
if (this.isEmpty()) {
throw new Error('Stack is empty');
}

if (this.top) {
return this.top.val;
}

return null;
}

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