Skip to content

feat: Added comment to Stack Implementation for LeetCode Java by Yemi. #1

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.codedifferently.lesson12;

public class Lesson12 {

/**
* Provide the solution to LeetCode 3062 here:
* 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 result[] = new int[] {0, 0};
while (head.next != null) {
if (head.val != head.next.val && head.val % 2 == 0) {
if (head.val > head.next.val) result[head.val % 2] += 1;
else result[head.next.val % 2] += 1;
}
head = head.next;
}
return (result[0] == result[1]) ? "Tie" : (result[0] > result[1] ? "Even" : "Odd");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.codedifferently.lesson12;

/** Implement the below Stack by providing code for the class methods. */
public class Stack {
private ListNode top;

Expand All @@ -9,18 +8,30 @@ 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;
int topmostValue = 0;
if (isEmpty()) {
throw new IllegalStateException("Stack is empty");
} else {
topmostValue = top.val;
top = top.next;
return topmostValue;
}
}

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

public boolean isEmpty() {
return true;
return top == null;
}
}
25 changes: 18 additions & 7 deletions lesson_12/structs_ts/src/lesson12.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { ListNode } from './list_node.js';

export class Lesson12 {
/**
* Provide the solution to LeetCode 3062 here:
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
*/
public gameResult(head: ListNode | null): string {
return '';
}
}
const result: number[] = [0, 0];
let current: ListNode | null = head;

while (current && current.next) {
if (current.val !== current.next.val && current.val % 2 === 0) {
if (current.val > current.next.val) {
result[current.val % 2] += 1;
} else {
result[current.next.val % 2] += 1;
}
}
current = current.next;
}
return (result[0] == result[1]) ? 'Tie' : (result[0] > result[1] ? 'Even' : 'Odd');
}

}

21 changes: 16 additions & 5 deletions lesson_12/structs_ts/src/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,29 @@ 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');
let topmostValue = undefined;
if (this.isEmpty())
throw new Error('No value to pop');
else
topmostValue = this.top?.val;
this.top = this.top?.next;
return topmostValue
}

peek(): number | null {
throw new Error('Not implemented');
if (this.top == undefined)
throw new Error('No value to pop');
else
return this.top.val;
}

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