Skip to content

Commit 60bd569

Browse files
committed
lesson_12_homework_structs_ts
1 parent ac458a6 commit 60bd569

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,24 @@ 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[] scores = {0, 0};
11+
while (head != null && head.next != null) {
12+
if (head.val != head.next.val && head.val % 2 == 0) {
13+
if (head.val > head.next.val) {
14+
scores[head.val % 2] += 1;
15+
} else {
16+
scores[head.next.val % 2] += 1;
17+
}
18+
}
19+
head = head.next;
20+
}
21+
22+
if (scores[0] == scores[1]) {
23+
return "Tie";
24+
} else if (scores[0] > scores[1]) {
25+
return "Even";
26+
} else {
27+
return "Odd";
28+
}
1129
}
1230
}

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

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

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

1517
public int pop() {
16-
return 0;
18+
if (isEmpty()) {
19+
throw new NullPointerException("Stack is empty");
20+
}
21+
int poppedValue = top.val;
22+
top = top.next;
23+
return poppedValue;
1724
}
1825

1926
public int peek() {
20-
return 0;
27+
if (isEmpty()) {
28+
throw new NullPointerException("Stack is empty");
29+
}
30+
return top.val;
2131
}
2232

2333
public boolean isEmpty() {
24-
return true;
34+
return top == null;
2535
}
2636
}

0 commit comments

Comments
 (0)