Skip to content

Commit 307239a

Browse files
committed
feat: adds Java and TypeScript Implementation
1 parent ac458a6 commit 307239a

File tree

4 files changed

+73
-9
lines changed

4 files changed

+73
-9
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,28 @@ public class Lesson12 {
77
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
88
*/
99
public String gameResult(ListNode head) {
10-
return null;
10+
int evenScore = 0, oddScore = 0;
11+
ListNode curr = head;
12+
13+
while (curr != null) {
14+
int evenVal = curr.val;
15+
int oddVal = curr.next.val;
16+
17+
if (evenVal > oddVal) {
18+
evenScore++;
19+
} else {
20+
oddScore++;
21+
}
22+
23+
curr = curr.next.next;
24+
}
25+
26+
if (evenScore > oddScore) {
27+
return "Even";
28+
} else if (oddScore > evenScore) {
29+
return "Odd";
30+
} else {
31+
return "Tie";
32+
}
1133
}
1234
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,25 @@ public Stack() {
1010

1111
public void push(int value) {
1212
// Your code here
13+
ListNode newNode = new ListNode(value);
14+
newNode.next = top;
15+
top = newNode;
1316
}
1417

1518
public int pop() {
16-
return 0;
19+
int val = top.val;
20+
top = top.next;
21+
return val;
1722
}
1823

1924
public int peek() {
20-
return 0;
25+
if (isEmpty()) {
26+
throw new IllegalStateException("Empty List");
27+
}
28+
return top.val;
2129
}
2230

2331
public boolean isEmpty() {
24-
return true;
32+
return top == null;
2533
}
2634
}

lesson_12/structs_ts/src/lesson12.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,27 @@ 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 evenScore = 0,
10+
oddScore = 0;
11+
let curr = head;
12+
13+
while (curr !== null && curr.next !== undefined) {
14+
const evenVal = curr.val;
15+
const oddVal = curr.next.val;
16+
17+
if (evenVal > oddVal) {
18+
evenScore++;
19+
} else {
20+
oddScore++;
21+
}
22+
23+
curr = curr.next?.next ?? null;
24+
}
25+
if (evenScore > oddScore) {
26+
return 'Even';
27+
} else if (oddScore > evenScore) {
28+
return 'Odd';
29+
}
30+
return 'Tie';
1031
}
1132
}

lesson_12/structs_ts/src/stack.ts

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

1010
push(value: number): void {
11-
throw new Error('Not implemented');
11+
const newNode: ListNode | undefined = 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+
if (this.top === null) {
18+
return undefined;
19+
}
20+
const val = this.top?.val;
21+
this.top = this.top?.next;
22+
return val;
1623
}
1724

1825
peek(): number | null {
19-
throw new Error('Not implemented');
26+
if (this.top === null) {
27+
throw new Error('Empty List');
28+
}
29+
if (this.top !== undefined) {
30+
return this.top.val;
31+
}
32+
throw new Error('Empty List');
2033
}
2134

2235
isEmpty(): boolean {
23-
throw new Error('Not implemented');
36+
return this.top === undefined;
2437
}
2538
}

0 commit comments

Comments
 (0)