Skip to content

Commit 2e5eec7

Browse files
committed
lfeat: lesson_12 and stack solutions in java
1 parent 439780a commit 2e5eec7

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ 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+
while (head != null && head.next != null) {
13+
if (head.val > head.next.val) {
14+
even++;
15+
} else {
16+
odd++;
17+
}
18+
head = head.next.next;
19+
}
20+
if (even == odd) {
21+
return ("Tie");
22+
} else if (even > odd) {
23+
return ("Even");
24+
} else {
25+
return ("Odd");
26+
}
1127
}
1228
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,23 @@ public Stack() {
1010

1111
public void push(int value) {
1212
// Your code here
13+
ListNode top2 = new ListNode(value);
14+
top2.next = top;
15+
top = top2;
1316
}
1417

1518
public int pop() {
16-
return 0;
19+
int topvalue = top.val;
20+
top = top.next;
21+
22+
return topvalue;
1723
}
1824

1925
public int peek() {
20-
return 0;
26+
return top.val;
2127
}
2228

2329
public boolean isEmpty() {
24-
return true;
30+
return top == null
2531
}
2632
}

0 commit comments

Comments
 (0)