diff --git a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java index af7663e90..60872114f 100644 --- a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java +++ b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java @@ -2,11 +2,28 @@ public class Lesson12 { + public 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; + ListNode current = head; + int oddPoints = 0; + int evenPoints = 0; + while (current != null) { + if (current.val > current.next.val) { + evenPoints++; + } else { + oddPoints++; + } + current = current.next.next; + } + if (evenPoints > oddPoints) { + return "Even"; + } else if (evenPoints < oddPoints) { + return "Odd"; + } else return "Tie"; } } diff --git a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Stack.java b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Stack.java index 8444fceca..825110ad0 100644 --- a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Stack.java +++ b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Stack.java @@ -9,18 +9,22 @@ public Stack() { } public void push(int value) { - // Your code here + ListNode pushedVal = new ListNode(value); + pushedVal.next = top; + top = pushedVal; } public int pop() { - return 0; + int val = top.val; + top = top.next; + return val; } public int peek() { - return 0; + return top.val; } public boolean isEmpty() { - return true; + return top == null; } } diff --git a/lesson_12/structs_ts/src/lesson12.ts b/lesson_12/structs_ts/src/lesson12.ts index d4455564e..9d3064152 100644 --- a/lesson_12/structs_ts/src/lesson12.ts +++ b/lesson_12/structs_ts/src/lesson12.ts @@ -6,6 +6,36 @@ 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 current = head; + let oddPoints = 0; + let evenPoints = 0; + + while (current != null && current.next != null) { + if (current.next.next == null) { + if (current.val > current.next.val) { + evenPoints++; + break; + } else { + oddPoints++; + break; + } + } else { + if (current.val > current.next.val) { + evenPoints++; + current = current.next.next; + } else { + oddPoints++; + current = current.next.next; + } + } + } + + if (oddPoints > evenPoints) { + return 'Odd'; + } else if (oddPoints < evenPoints) { + return 'Even'; + } else { + return 'Tie'; + } } } diff --git a/lesson_12/structs_ts/src/stack.ts b/lesson_12/structs_ts/src/stack.ts index b931ec0ef..0971b0406 100644 --- a/lesson_12/structs_ts/src/stack.ts +++ b/lesson_12/structs_ts/src/stack.ts @@ -8,18 +8,26 @@ export class Stack { } push(value: number): void { - throw new Error('Not implemented'); + const pusher = new ListNode(value); + pusher.next = this.top; + this.top = pusher; } pop(): number | undefined { - throw new Error('Not implemented'); + const val = this.top?.val; + this.top = this.top?.next; + return val; } peek(): number | null { - throw new Error('Not implemented'); + if (this.top != null) { + return this.top.val; + } else { + throw new Error('empty stack'); + } } isEmpty(): boolean { - throw new Error('Not implemented'); + return this.top == null; } }