File tree Expand file tree Collapse file tree 4 files changed +73
-9
lines changed
structs_java/structs_app/src/main/java/com/codedifferently/lesson12 Expand file tree Collapse file tree 4 files changed +73
-9
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,28 @@ public class Lesson12 {
77 * https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
88 */
99 public String gameResult (ListNode head ) {
10- return null ;
10+ int evenScore = 0 , oddScore = 0 ;
11+ ListNode curr = head ;
12+
13+ while (curr != null ) {
14+ int evenVal = curr .val ;
15+ int oddVal = curr .next .val ;
16+
17+ if (evenVal > oddVal ) {
18+ evenScore ++;
19+ } else {
20+ oddScore ++;
21+ }
22+
23+ curr = curr .next .next ;
24+ }
25+
26+ if (evenScore > oddScore ) {
27+ return "Even" ;
28+ } else if (oddScore > evenScore ) {
29+ return "Odd" ;
30+ } else {
31+ return "Tie" ;
32+ }
1133 }
1234}
Original file line number Diff line number Diff line change @@ -10,17 +10,25 @@ public Stack() {
1010
1111 public void push (int value ) {
1212 // Your code here
13+ ListNode newNode = new ListNode (value );
14+ newNode .next = top ;
15+ top = newNode ;
1316 }
1417
1518 public int pop () {
16- return 0 ;
19+ int val = top .val ;
20+ top = top .next ;
21+ return val ;
1722 }
1823
1924 public int peek () {
20- return 0 ;
25+ if (isEmpty ()) {
26+ throw new IllegalStateException ("Empty List" );
27+ }
28+ return top .val ;
2129 }
2230
2331 public boolean isEmpty () {
24- return true ;
32+ return top == null ;
2533 }
2634}
Original file line number Diff line number Diff line change @@ -6,6 +6,27 @@ 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+ let evenScore = 0 ,
10+ oddScore = 0 ;
11+ let curr = head ;
12+
13+ while ( curr !== null && curr . next !== undefined ) {
14+ const evenVal = curr . val ;
15+ const oddVal = curr . next . val ;
16+
17+ if ( evenVal > oddVal ) {
18+ evenScore ++ ;
19+ } else {
20+ oddScore ++ ;
21+ }
22+
23+ curr = curr . next ?. next ?? null ;
24+ }
25+ if ( evenScore > oddScore ) {
26+ return 'Even' ;
27+ } else if ( oddScore > evenScore ) {
28+ return 'Odd' ;
29+ }
30+ return 'Tie' ;
1031 }
1132}
Original file line number Diff line number Diff line change @@ -8,18 +8,31 @@ export class Stack {
88 }
99
1010 push ( value : number ) : void {
11- throw new Error ( 'Not implemented' ) ;
11+ const newNode : ListNode | undefined = 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 === null ) {
18+ return undefined ;
19+ }
20+ const val = this . top ?. val ;
21+ this . top = this . top ?. next ;
22+ return val ;
1623 }
1724
1825 peek ( ) : number | null {
19- throw new Error ( 'Not implemented' ) ;
26+ if ( this . top === null ) {
27+ throw new Error ( 'Empty List' ) ;
28+ }
29+ if ( this . top !== undefined ) {
30+ return this . top . val ;
31+ }
32+ throw new Error ( 'Empty List' ) ;
2033 }
2134
2235 isEmpty ( ) : boolean {
23- throw new Error ( 'Not implemented' ) ;
36+ return this . top === undefined ;
2437 }
2538}
You can’t perform that action at this time.
0 commit comments