目录
1.链表的操作
1.1链表反转
需求:
原链表中数据为:1->2->3>4
反转后链表中数据为:4->3->2->1
public void reverse(){
if (N==0){
//当前是空链表,不需要反转
return;
}
reverse(head.next);
}
/**
*
* @param curr 当前遍历的结点
* @return 反转后当前结点上一个结点
*/
public Node reverse(Node curr){
//到达最后一个节点
if (curr.next == null){
//反转后,头结点应该指向原链表中的最后一个元素
head.next = curr;
return curr;
}
//当前结点的上一个结点
Node pre = reverse(curr.next);
pre.next = curr;
//当前结点的下一个结点设为null
curr.next = null;
//返回当前结点
return curr;
}
1.2快慢指针
1.2.1 中间值问题
/**
* @param first 链表的首结点
* @return 链表的中间结点的值
*/
public static String getMid(Node<String> first) {
Node<String> slow = first;
Node<String> fast = first;
while (fast!=null && fast.next!=null){
fast = fast.next.next;
slow = slow.next;
}
return slow.item;
}
1.2.2单向链路是否有环
public static boolean isCircle(Node<String> first) {
Node<String> slow = first;
Node<String> fast = first;
while(fast!=null && fast.next!=null){
fast = fast.next.next;
slow = slow.next;
if (fast.equals(slow)){
return true;
}
}
return false;
}
1.2.3有环链表入口问题
public static Node getEntrance(Node<String> first) {
Node<String> slow = first;
Node<String> fast = first;
Node<String> temp = null;
while(fast!=null && fast.next!=null){
fast = fast.next.next;
slow = slow.next;
if (fast.equals(slow)){
temp = first;
//return true;
continue;
}
if (temp!=null){
temp=temp.next;
if (temp.equals(slow)){
return temp;
}
}
}
return null;
}
2.栈
2.1栈的API实现
2.2API代码实现
import java.util.Iterator;
public class Stack<T> implements Iterable<T>{
//记录首结点
private Node head;
//栈中元素的个数
private int N;
public Stack() {
head = new Node(null,null);
N=0;
}
//判断当前栈中元素个数是否为0
public boolean isEmpty(){
return N==0;
}
//把t元素压入栈
public void push(T t){
Node oldNext = head.next;
Node node = new Node(t, oldNext);
head.next = node;
//个数+1
N++;
}
//弹出栈顶元素
public T pop(){
Node oldNext = head.next;
if (oldNext==null){
return null;
}
//删除首个元素
head.next = head.next.next;
//个数-1
N--;
return oldNext.item;
}
//获取栈中元素的个数
public int size(){
return N;
}
@Override
public Iterator<T> iterator() {
return new SIterator();
}
private class SIterator implements Iterator<T>{
private Node n = head;
@Override
public boolean hasNext() {
return n.next!=null;
}
@Override
public T next() {
Node node = n.next;
n = n.next;
return node.item;
}
}
private class Node{
public T item;
public Node next;
public Node(T item, Node next) {
this.item = item;
this.next = next;
}
}
}
2.3队列的API设计
2.4队列的API实现
package linear;
import java.util.Iterator;
public class Queue<T> implements Iterable<T>{
//记录首结点
private Node head;
//记录最后一个结点
private Node last;
//记录队列中元素的个数
private int N;
public Queue() {
head = new Node(null,null);
last=null;
N=0;
}
//判断队列是否为空
public boolean isEmpty(){
return N==0;
}
//返回队列中元素的个数
public int size(){
return N;
}
//向队列中插入元素t
public void enqueue(T t){
if (last==null){
last = new Node(t,null);
head.next=last;
}else{
Node oldLast = last;
last = new Node(t,null);
oldLast.next=last;
}
//个数+1
N++;
}
//从队列中拿出一个元素
public T dequeue(){
if (isEmpty()){
return null;
}
Node oldFirst = head.next;
head.next = oldFirst.next;
N--;
if (isEmpty()){
last=null;
}
return oldFirst.item;
}
@Override
public Iterator<T> iterator() {
return new QIterator();
}
private class QIterator implements Iterator<T>{
private Node n = head;
@Override
public boolean hasNext() {
return n.next!=null;
}
@Override
public T next() {
Node node = n.next;
n = n.next;
return node.item;
}
}
private class Node{
public T item;
public Node next;
public Node(T item, Node next) {
this.item = item;
this.next = next;
}
}
}