Skip to content

feat: added Lesson12's Java and Typescript files for Chelsea #401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ public class Lesson12 {
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
*/
public String gameResult(ListNode head) {
return null;
int oddPoints = 0;
int evenPoints = 0;

for (ListNode current = head;
current != null && current.next != null;
current = current.next.next) {
if (current.val > current.next.val) {
evenPoints++;
} else {
oddPoints++;
}
}
if (oddPoints > evenPoints) {
return "Odd";
} else if (evenPoints > oddPoints) {
return "Even";
} else {
return "Tie";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,28 @@ public Stack() {
}

public void push(int value) {
// Your code here
ListNode newNode = new ListNode(value);
newNode.next = top;
top = newNode;
}

public int pop() {
return 0;
if (isEmpty()) {
throw new IllegalStateException("Stack is empty");
}
int value = top.val;
top = top.next;
return value;
}

public int peek() {
return 0;
if (isEmpty()) {
throw new IllegalStateException("Stack is empty");
}
return top.val;
}

public boolean isEmpty() {
return true;
return top == null;
}
}
3 changes: 1 addition & 2 deletions lesson_12/structs_ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,5 @@
"tsc-watch": "^6.2.0",
"typescript": "^5.6.2",
"typescript-eslint": "^8.7.0"
},
"dependencies": {}
}
}
21 changes: 20 additions & 1 deletion lesson_12/structs_ts/src/lesson12.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ export class Lesson12 {
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
*/
public gameResult(head: ListNode | null): string {
return '';
let oddPoints = 0;
let evenPoints = 0;
let current: ListNode | null = head;

while (current && current.next) {
if (current.val > current.next.val) {
evenPoints++;
} else {
oddPoints++;
}
current = current.next.next || null;
}

if (oddPoints > evenPoints) {
return 'Odd';
} else if (evenPoints > oddPoints) {
return 'Even';
} else {
return 'Tie';
}
}
}
18 changes: 14 additions & 4 deletions lesson_12/structs_ts/src/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,28 @@ export class Stack {
}

push(value: number): void {
throw new Error('Not implemented');
const newNode = new ListNode(value);
newNode.next = this.top;
this.top = newNode;
}

pop(): number | undefined {
throw new Error('Not implemented');
if (this.top === undefined) {
return undefined;
}
const poppedNode = this.top;
this.top = this.top.next;
return poppedNode.val;
}

peek(): number | null {
throw new Error('Not implemented');
if (this.top === undefined) {
throw new Error('Stack is empty');
}
return this.top.val;
}

isEmpty(): boolean {
throw new Error('Not implemented');
return this.top === undefined;
}
}