Skip to content

Commit 0362fcb

Browse files
author
“A1-4U2T1NN”
committed
feat: added stack code while trying to fix a testing error;
1 parent aab1baa commit 0362fcb

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed
Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.codedifferently.lesson12;
22

3-
43
public class Lesson12 {
54

65

@@ -9,21 +8,23 @@ public class Lesson12 {
98
* https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game
109
*/
1110
public String gameResult(ListNode head) {
12-
int evenCounter = 0; //Keeps track of even score
13-
int oddCounter = 0; //Keeps track of odd score
14-
11+
int evenCounter = 0; //Keeps track of even score
12+
int oddCounter = 0; //Keeps track of odd score
1513

16-
int evenValue = head.val; //Makes the first even value to compare equal to the first number of the list
17-
int oddValue = head.next.val; //Makes the first odd value to compare equal to the second number of the list
14+
while(head != null && head.next != null) { //Goes through the Linked List until theres no more pairs
15+
int evenValue = head.val; //Makes the first even value to compare equal to the first number of the list
16+
int oddValue = head.next.val; //Makes the first odd value to compare equal to the second number of the list
1817

1918

20-
if ( evenValue > oddValue ) {
21-
evenCounter = evenCounter + 1;
22-
//Compares the even and odd value, adds 1 to even score if even is greater
23-
} else {
24-
oddCounter = ++oddCounter + 1;
25-
} //Compares the even and odd value, adds 1 to odd score if odd is greater
26-
19+
if ( evenValue > oddValue ) {
20+
evenCounter = evenCounter + 1;
21+
//Compares the even and odd value, adds 1 to even score if even is greater
22+
} else {
23+
oddCounter = oddCounter + 1;
24+
} //Compares the even and odd value, adds 1 to odd score if odd is greater
25+
// head.val = head.next.val; //Brings out the next pair befor restarting the loop
26+
head = head.next; //Brings out the next pair befor restarting the loop
27+
}
2728

2829
if (evenCounter > oddCounter) {
2930
return "Even";
@@ -34,7 +35,7 @@ public String gameResult(ListNode head) {
3435
} else {
3536
return "Tie";
3637
} //Compares the even and odd score, prints 'Tie' if the two scores are equal
37-
38+
3839
}
3940

4041
}

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

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

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

1517
public int pop() {
16-
return 0;
18+
int newTop = 0;
19+
if(isEmpty()){
20+
throw new IllegalStateException("Stack is empty");
21+
}else{
22+
newTop = top.val;
23+
top = top.next;
24+
return newTop;
25+
}
1726
}
1827

1928
public int peek() {
20-
return 0;
29+
if(isEmpty()){
30+
throw new IllegalStateException("Stack is empty");
31+
}
32+
return top.val;
2133
}
2234

2335
public boolean isEmpty() {
24-
return true;
36+
return top == null;
2537
}
2638
}

0 commit comments

Comments
 (0)