File tree Expand file tree Collapse file tree 3 files changed +59
-9
lines changed
structs_java/structs_app/src/main/java/com/codedifferently/lesson12 Expand file tree Collapse file tree 3 files changed +59
-9
lines changed Original file line number Diff line number Diff line change 11package com .codedifferently .lesson12 ;
22
3+ import java .util .EmptyStackException ;
4+
35/** Implement the below Stack by providing code for the class methods. */
46public class Stack {
57 private ListNode top ;
@@ -9,18 +11,30 @@ public Stack() {
911 }
1012
1113 public void push (int value ) {
12- // Your code here
14+ ListNode newNode = new ListNode (value );
15+ newNode .next = top ;
16+ top = newNode ;
1317 }
1418
1519 public int pop () {
16- return 0 ;
20+ if (top == null ) {
21+ throw new EmptyStackException ();
22+ }
23+ int poppedval = top .val ;
24+ top = top .next ;
25+ return poppedval ;
1726 }
1827
1928 public int peek () {
20- return 0 ;
29+ if (top == null ) {
30+ throw new EmptyStackException ();
31+ }
32+ return top .val ;
2133 }
2234
2335 public boolean isEmpty () {
24- return true ;
36+ if (top == null ) {
37+ return true ;
38+ } else return false ;
2539 }
2640}
Original file line number Diff line number Diff line change @@ -6,6 +6,28 @@ 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+ const ls : number [ ] = [ 0 , 0 ] ; //Sets list to only represent numbers
10+ while ( head && head . next ) {
11+ //Goes through linked list until there are no more numbers
12+ if ( head . val !== head . next . val && head . val % 2 === 0 ) {
13+ //determines if we have different numbers as well as if head number is divisible by 2
14+ if ( head . val > head . next . val ) {
15+ //Checks if the number was even and is greater than second number
16+ ls [ head . val % 2 ] += 1 ; //If both were true this function occurs and gives even a point
17+ } else {
18+ ls [ head . next . val % 2 ] += 1 ; //If either are false then give odd a point
19+ }
20+ }
21+ head = head . next ; //Proceed to next node/set of numbers to continue unil we are out of pairs
22+ }
23+
24+ if ( ls [ 0 ] > ls [ 1 ] ) {
25+ //Tells us the result based on which list[] got the point vvv
26+ return 'Even' ;
27+ } else if ( ls [ 0 ] === ls [ 1 ] ) {
28+ return 'Tie' ;
29+ } else {
30+ return 'Odd' ;
31+ }
1032 }
1133}
Original file line number Diff line number Diff line change @@ -8,18 +8,32 @@ export class Stack {
88 }
99
1010 push ( value : number ) : void {
11- throw new Error ( 'Not implemented' ) ;
11+ const newNode = 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 === undefined ) {
18+ return undefined ;
19+ }
20+ const poppedValue = this . top . val ;
21+ this . top = this . top . next ;
22+ return poppedValue ;
1623 }
1724
1825 peek ( ) : number | null {
19- throw new Error ( 'Not implemented' ) ;
26+ if ( this . top === undefined ) {
27+ throw new Error ( 'Stack is empty' ) ;
28+ }
29+ return this . top . val ;
2030 }
2131
2232 isEmpty ( ) : boolean {
23- throw new Error ( 'Not implemented' ) ;
33+ if ( this . top === undefined ) {
34+ return true ;
35+ } else {
36+ return false ;
37+ }
2438 }
2539}
You can’t perform that action at this time.
0 commit comments