Skip to content

Commit 2eab061

Browse files
author
jjcapparell
committed
feat: extra credit: typescript implementations added
1 parent 22cae82 commit 2eab061

File tree

2 files changed

+72
-13
lines changed

2 files changed

+72
-13
lines changed

lesson_12/structs_ts/src/lesson12.ts

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

33
export class Lesson12 {
4-
/**
5-
* Provide the solution to LeetCode 3062 here:
6-
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
7-
*/
8-
public gameResult(head: ListNode | null): string {
9-
return '';
4+
public gameResult(head: ListNode): string {
5+
let scoreEven = 0;
6+
let scoreOdd = 0;
7+
let evenNum = 0;
8+
let oddNum = 0;
9+
const length = this.getLength(head);
10+
11+
for (let i = 0; i < length; i++) {
12+
const resultNode = this.getElementAt(head, i);
13+
if (resultNode) {
14+
if (i % 2 === 0) {
15+
evenNum = resultNode.val;
16+
} else {
17+
oddNum = resultNode.val;
18+
if (evenNum > oddNum) {
19+
scoreEven += 1;
20+
} else if (evenNum < oddNum) {
21+
scoreOdd += 1;
22+
}
23+
}
24+
}
25+
}
26+
27+
if (scoreEven > scoreOdd) {
28+
return "Even";
29+
} else if (scoreEven < scoreOdd) {
30+
return "Odd";
31+
} else {
32+
return "Tie";
33+
}
34+
}
35+
36+
getElementAt(head: ListNode | undefined, position: number): ListNode | undefined {
37+
let current = head;
38+
let index = 0;
39+
40+
while (current !== undefined) {
41+
if (index === position) {
42+
return current; // Return the node at the specified position
43+
}
44+
current = current.next; // Move to the next node
45+
index++; // Increment the index
46+
}
47+
48+
return undefined; // Return null if the position is out of bounds
49+
}
50+
51+
getLength(head: ListNode | undefined): number {
52+
let length = 0;
53+
let current = head;
54+
55+
while (current !== undefined) {
56+
length++; // Increment to keep count through each element
57+
current = current.next; // Move on to the next element
58+
}
59+
return length;
1060
}
1161
}

lesson_12/structs_ts/src/stack.ts

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

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

66
constructor() {
77
this.top = undefined;
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

14-
pop(): number | undefined {
15-
throw new Error('Not implemented');
15+
pop(): number {
16+
if (this.isEmpty()) {
17+
throw new Error("Stack is empty");
18+
}
19+
const value = this.top!.val; // Non-null assertion since we've checked for emptiness
20+
this.top = this.top?.next; // No change needed here, TypeScript allows accessing next
21+
return value;
1622
}
1723

18-
peek(): number | null {
19-
throw new Error('Not implemented');
24+
peek(): number {
25+
if (this.isEmpty()) {
26+
throw new Error("Stack is empty");
27+
}
28+
return this.top!.val; // Non-null assertion since we've checked for emptiness
2029
}
2130

2231
isEmpty(): boolean {
23-
throw new Error('Not implemented');
32+
return this.top === undefined; // Check for undefined since top is optional
2433
}
2534
}

0 commit comments

Comments
 (0)