Skip to content

Commit e9c5deb

Browse files
author
Yafiaha
committed
Feat : Yafiah Lesson-12 Game Result LeetCode & Implement Stack.java
1 parent 439780a commit e9c5deb

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-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 even = 0;
11+
int odd = 0;
12+
13+
while(head != null && head.next != null){
14+
if (head.val > head.next.val) {
15+
even++;
16+
} else if(head.val < head.next.val) {
17+
odd++;
18+
}
19+
head = head.next.next;
20+
}
21+
22+
if(even > odd){
23+
return "Even";
24+
}
25+
else if(even < odd ){
26+
return "Odd";
27+
}
28+
return "Tie";
1129
}
1230
}

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

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

1111
public void push(int value) {
12-
// Your code here
12+
top = new ListNode(value, top);
1313
}
1414

1515
public int pop() {
16-
return 0;
16+
int value = top.val;
17+
top = top.next;
18+
return value;
1719
}
1820

1921
public int peek() {
20-
return 0;
22+
return top.val;
2123
}
2224

2325
public boolean isEmpty() {
24-
return true;
26+
return top == null;
2527
}
2628
}

0 commit comments

Comments
 (0)