Skip to content

Commit 151d59c

Browse files
feat: added Stack impl/ LeetCode 3062 finished in TypeScript
1 parent 439780a commit 151d59c

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

lesson_12/structs_ts/src/lesson12.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,32 @@ 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 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";
1036
}
1137
}

lesson_12/structs_ts/src/stack.ts

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

1010
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+
1216
}
1317

1418
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;
1630
}
1731

1832
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+
2043
}
2144

2245
isEmpty(): boolean {
23-
throw new Error('Not implemented');
46+
return top === undefined;
2447
}
2548
}

0 commit comments

Comments
 (0)