Skip to content

Commit 719470b

Browse files
committed
feat: implement gameResult method and complete Stack operations in TypeScript
1 parent 4b70271 commit 719470b

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-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+
const scoreBoard = [0, 0];
10+
11+
while (head?.next != null) {
12+
if(head?.val != head?.next?.val && head?.val % 2 == 0) {
13+
if(head?.val > head?.next.val) {
14+
scoreBoard[head?.val % 2] += 1;
15+
} else {
16+
scoreBoard[head?.next.val % 2] +=1;
17+
}
18+
}
19+
head = head?.next;
20+
}
21+
22+
if (scoreBoard[0] == scoreBoard[1]) {
23+
return 'Tie';
24+
} else if (scoreBoard[0] > scoreBoard[1]) {
25+
return 'Even';
26+
} else {
27+
return 'Odd';
28+
}
1029
}
1130
}

lesson_12/structs_ts/src/stack.ts

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

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

1416
pop(): number | undefined {
15-
throw new Error('Not implemented');
17+
const current = this.top;
18+
19+
this.top = this.top?.next;
20+
21+
return current?.val;
1622
}
1723

1824
peek(): number | null {
19-
throw new Error('Not implemented');
25+
if (this.top === undefined) {
26+
throw new Error('Invalid Sytanx');
27+
}
28+
return this.top.val;
2029
}
2130

2231
isEmpty(): boolean {
23-
throw new Error('Not implemented');
32+
return this.top == null;
2433
}
2534
}

0 commit comments

Comments
 (0)