Skip to content

Commit 12bce4e

Browse files
committed
fix: changed Java files and added Typescript files
1 parent 9c63e5f commit 12bce4e

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@ public String gameResult(ListNode head) {
1010
int oddPoints = 0;
1111
int evenPoints = 0;
1212

13-
ListNode current = head;
14-
15-
while (current != null && current.next != null) {
13+
for (ListNode current = head;
14+
current != null && current.next != null;
15+
current = current.next.next) {
1616
if (current.val > current.next.val) {
1717
evenPoints++;
1818
} else {
1919
oddPoints++;
2020
}
21-
current = current.next.next;
2221
}
23-
2422
if (oddPoints > evenPoints) {
2523
return "Odd";
2624
} else if (evenPoints > oddPoints) {

lesson_12/structs_ts/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,5 @@
3434
"tsc-watch": "^6.2.0",
3535
"typescript": "^5.6.2",
3636
"typescript-eslint": "^8.7.0"
37-
},
38-
"dependencies": {}
37+
}
3938
}

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+
let oddPoints = 0;
10+
let evenPoints = 0;
11+
let current: ListNode | null = head;
12+
13+
while (current && current.next) {
14+
if (current.val > current.next.val) {
15+
evenPoints++;
16+
} else {
17+
oddPoints++;
18+
}
19+
current = current.next.next || null;
20+
}
21+
22+
if (oddPoints > evenPoints) {
23+
return "Odd";
24+
} else if (evenPoints > oddPoints) {
25+
return "Even";
26+
} else {
27+
return "Tie";
28+
}
1029
}
1130
}

lesson_12/structs_ts/src/stack.ts

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

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

66
constructor() {
7-
this.top = undefined;
7+
this.top = null;
88
}
99

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

1416
pop(): number | undefined {
15-
throw new Error('Not implemented');
17+
if (this.isEmpty()) {
18+
throw new Error('Stack is empty');
19+
}
20+
const value = this.top!.val;
21+
this.top = this.top!.next || null;
22+
return value;
1623
}
1724

1825
peek(): number | null {
19-
throw new Error('Not implemented');
26+
if (this.isEmpty()) {
27+
throw new Error('Stack is empty');
28+
}
29+
return this.top!.val;
2030
}
2131

2232
isEmpty(): boolean {
23-
throw new Error('Not implemented');
33+
return this.top === null;
2434
}
2535
}

0 commit comments

Comments
 (0)