Implement Stack by LinkedList
public class Stack {
private ListNode head;
private int size;
public Stack() {
this.head = null;
this.size = 0;
}
public Integer pop() {
if (head == null) {
return null;
}
ListNode result = head;
head = head.next;
result.next = null;
return result.value;
}
public Integer peek() {
if (head == null) {
return null;
}
return head.value;
}
public boolean push(int element) {
ListNode newNode = new ListNode(element);
newNode.next = head;
head = newNode;
size++;
return true;
}
public int size() {
return size;
}
public boolean isEmepty() {
return size == 0;
}
}Last updated