Skip to content

Commit ed246e2

Browse files
committed
changes made to lesson 12 stack and java.
1 parent 439780a commit ed246e2

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ 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+
11+
ListNode current = head;
12+
int evenTeam = 0;
13+
int oddTeam = 0;
14+
while (current != null) {
15+
if (current.val < current.next.val) {
16+
oddTeam++;
17+
} else if (current.val > current.next.val) {
18+
evenTeam++;
19+
}
20+
current = current.next.next;
21+
}
22+
23+
if (evenTeam < oddTeam) {
24+
return "oddTeamwins";
25+
} else if (evenTeam > oddTeam) {
26+
return "eveTeamwins";
27+
} else return "tie";
1128
}
1229
}

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 first = new ListNode(value);
13+
first.next = top;
14+
top = first;
1315
}
1416

1517
public int pop() {
16-
return 0;
18+
int value = top.val;
19+
top = top.next;
20+
return value;
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
}

0 commit comments

Comments
 (0)