插入和删除O(1)
public class QueueLinkedList {
private int size;
private Node head;
private Node tail;
public QueueLinkedList(){
size = 0;
head = new Node(0);
tail = new Node(0);
head.next = tail;
}
public void push(int val){
Node node = new Node(0);
tail.val = val;
tail.next = node;
tail = tail.next;
size++;
}
public int pop(){
if(size == 0) return -1;
int a = head.next.val;
head = head.next;
size--;
System.out.println(a);
return a;
}
}
class Node{
Node next;
int val;
public Node(int val){
this.val = val;
}
}