Skip to content

Commit 36a7142

Browse files
committed
Feat: MMakes changes to Lesson12.java, Stack.java, lesson12.ts, and stack.ts with passing tests for lesson_12 homework and extra credit - Joseph Caballero
1 parent 39c8f59 commit 36a7142

File tree

4 files changed

+77
-12
lines changed

4 files changed

+77
-12
lines changed

lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,34 @@
22

33
public class Lesson12 {
44

5+
public Lesson12() {
6+
7+
}
8+
59
/**
610
* Provide the solution to LeetCode 3062 here:
711
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
812
*/
913
public String gameResult(ListNode head) {
10-
return null;
11-
}
12-
}
14+
ListNode current = head;
15+
int oddPoints = 0;
16+
int evenPoints = 0;
17+
while (current != null){
18+
if (current.val > current.next.val){
19+
evenPoints++;
20+
}
21+
else{
22+
oddPoints++;
23+
}
24+
current = current.next.next;
25+
}
26+
if ( evenPoints > oddPoints) {
27+
return "Even";
28+
} else if ( evenPoints<oddPoints) {
29+
return "Odd";
30+
}
31+
else return "Tie";
32+
33+
}
34+
35+
}

lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Stack.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@ public Stack() {
99
}
1010

1111
public void push(int value) {
12-
// Your code here
12+
ListNode pushedVal = new ListNode(value);
13+
pushedVal.next = top;
14+
top = pushedVal;
1315
}
1416

1517
public int pop() {
16-
return 0;
18+
int val = top.val;
19+
top = top.next;
20+
return val;
1721
}
1822

1923
public int peek() {
20-
return 0;
24+
return top.val;
2125
}
2226

2327
public boolean isEmpty() {
24-
return true;
28+
return top == null;
2529
}
2630
}

lesson_12/structs_ts/src/lesson12.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,36 @@ 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 current = head;
10+
let oddPoints = 0;
11+
let evenPoints = 0;
12+
13+
while (current != null && current.next != null) {
14+
if (current.next.next == null) {
15+
if (current.val > current.next.val) {
16+
evenPoints++;
17+
break;
18+
} else {
19+
oddPoints++;
20+
break;
21+
}
22+
} else {
23+
if (current.val > current.next.val) {
24+
evenPoints++;
25+
current = current.next.next;
26+
} else {
27+
oddPoints++;
28+
current = current.next.next;
29+
}
30+
}
31+
}
32+
33+
if (oddPoints > evenPoints) {
34+
return 'Odd';
35+
} else if (oddPoints < evenPoints) {
36+
return 'Even';
37+
} else {
38+
return 'Tie';
39+
}
1040
}
1141
}

lesson_12/structs_ts/src/stack.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,26 @@ export class Stack {
88
}
99

1010
push(value: number): void {
11-
throw new Error('Not implemented');
11+
const pusher = new ListNode(value);
12+
pusher.next = this.top;
13+
this.top = pusher;
1214
}
1315

1416
pop(): number | undefined {
15-
throw new Error('Not implemented');
17+
const val = this.top?.val;
18+
this.top = this.top?.next;
19+
return val;
1620
}
1721

1822
peek(): number | null {
19-
throw new Error('Not implemented');
23+
if (this.top != null) {
24+
return this.top.val;
25+
} else {
26+
throw new Error('empty stack');
27+
}
2028
}
2129

2230
isEmpty(): boolean {
23-
throw new Error('Not implemented');
31+
return this.top == null;
2432
}
2533
}

0 commit comments

Comments
 (0)