Skip to content

Commit cc90062

Browse files
committed
feat/fix: Fixed divergent branch issue. Changed the order of var declarations inpush method. logic was reversed. Also added exception handling.
1 parent 8d6467e commit cc90062

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed
Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,44 @@
11
package com.codedifferently.lesson12;
22

33
public class Lesson12 {
4+
// node1 node2 node3 node4 null
45

56
/**
67
* Provide the solution to LeetCode 3062 here:
78
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
89
*/
910
public String gameResult(ListNode head) {
10-
return null;
11+
// These vars keep track of the point for the even and odd indicies.
12+
int oddPoints = 0;
13+
int evenPoints = 0;
14+
15+
/*
16+
* This var will be redeclared throughout the loop and be used
17+
* to point to the node pairs.
18+
*/
19+
ListNode curr = head;
20+
21+
/**
22+
* This loop will do two comparisons. The 1st comparison is to determine which index has a
23+
* greater value. The 2nd comparison checks which index team is more points
24+
*/
25+
while (curr != null) {
26+
if (curr.val > curr.next.val) {
27+
evenPoints++;
28+
} else if (curr.val < curr.next.val) {
29+
oddPoints++;
30+
}
31+
32+
// "curr.next.next" allows for the loop to skip over an index
33+
curr = curr.next.next;
34+
}
35+
36+
if (oddPoints > evenPoints) {
37+
return "Odd";
38+
} else if (oddPoints < evenPoints) {
39+
return "Even";
40+
} else {
41+
return "Tie";
42+
}
1143
}
1244
}

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

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

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

1518
public int pop() {
16-
return 0;
19+
if (isEmpty()) {
20+
throw new IllegalStateException("Empty");
21+
}
22+
int oldTopVal = top.val;
23+
top = top.next;
24+
return oldTopVal;
1725
}
1826

1927
public int peek() {
20-
return 0;
28+
if (isEmpty()) {
29+
throw new IllegalStateException("Empty");
30+
}
31+
return top.val;
2132
}
2233

2334
public boolean isEmpty() {
24-
return true;
35+
return top == null;
2536
}
2637
}

0 commit comments

Comments
 (0)