Skip to content

Commit 62db949

Browse files
committed
feat: add lesson_12 extra credit
1 parent 2ef3138 commit 62db949

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

lesson_12/structs_ts/src/lesson12.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ 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 even = 0;
10+
let odd = 0;
11+
while (head != null && head.next != null) {
12+
if (head.val > head.next.val) {
13+
even++;
14+
} else {
15+
odd++;
16+
}
17+
head = head.next.next !== undefined ? head.next.next : null;
18+
}
19+
if (even === odd) {
20+
return 'Tie';
21+
} else if (even > odd) {
22+
return 'Even';
23+
} else {
24+
return 'Odd';
25+
}
1026
}
1127
}

lesson_12/structs_ts/src/stack.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,25 @@ 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 nodevalue = this.top?.val;
18+
this.top = this.top?.next;
19+
return nodevalue;
1620
}
1721

1822
peek(): number | null {
19-
throw new Error('Not implemented');
23+
if (this.top !== null && this.top !== undefined) {
24+
return this.top.val;
25+
}
26+
return null;
2027
}
2128

2229
isEmpty(): boolean {
23-
throw new Error('Not implemented');
30+
return this.top == null;
2431
}
2532
}

0 commit comments

Comments
 (0)