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..99db84127 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 @@ -7,6 +7,22 @@ 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 even = 0; + int odd = 0; + while (head != null && head.next != null) { + if (head.val > head.next.val) { + even++; + } else { + odd++; + } + head = head.next.next; + } + if (even == odd) { + return ("Tie"); + } else if (even > odd) { + return ("Even"); + } else { + return ("Odd"); + } } } 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..718468b79 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 @@ -10,17 +10,23 @@ public Stack() { public void push(int value) { // Your code here + ListNode top2 = new ListNode(value); + top2.next = top; + top = top2; } public int pop() { - return 0; + int topvalue = top.val; + top = top.next; + + return topvalue; } 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..ec0f96b48 100644 --- a/lesson_12/structs_ts/src/lesson12.ts +++ b/lesson_12/structs_ts/src/lesson12.ts @@ -6,6 +6,22 @@ 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 even = 0; + let odd = 0; + while (head != null && head.next != null) { + if (head.val > head.next.val) { + even++; + } else { + odd++; + } + head = head.next.next !== undefined ? head.next.next : null; + } + if (even === odd) { + return 'Tie'; + } else if (even > odd) { + return 'Even'; + } else { + return 'Odd'; + } } } diff --git a/lesson_12/structs_ts/src/stack.ts b/lesson_12/structs_ts/src/stack.ts index b931ec0ef..3874c9ffb 100644 --- a/lesson_12/structs_ts/src/stack.ts +++ b/lesson_12/structs_ts/src/stack.ts @@ -8,18 +8,24 @@ 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'); + const nodevalue = this.top?.val; + this.top = this.top?.next; + return nodevalue; } peek(): number | null { - throw new Error('Not implemented'); + if (this.top === undefined) { + throw new Error('is Empty'); + } + return this.top ? this.top.val : null; } - isEmpty(): boolean { - throw new Error('Not implemented'); + return this.top === undefined; } }