B站有个课,感觉可以一听
给两个再刷leetcode时的链接:
https://www.cnblogs.com/782687539-nanfu/p/10333031.html
剑指offer的6题是链表,一个很基础的可以看下
leecode中剑指offer09,用两个栈实现一个队列:
class CQueue {
Stack<Integer> st1;
Stack<Integer> st2;
public CQueue() {
st1 = new Stack<Integer>();
st2 = new Stack<Integer>();
}
public void appendTail(int value) {
st1.push(value);
}
public int deleteHead() {
int head=-1;
if (!st2.isEmpty()) {
head = st2.pop();
}else{
if(st1.isEmpty()){
return -1;
}else{
while(!st1.isEmpty()){
st2.push(st1.pop());
}
head = st2.pop();
}
}
return head;
}
}
首先注意一下如何写的构造函数
然后理解一下思路: