链表在空间是不连续的,包括:
public class Node {
    Object element; //数据域
    Node next;  //指针域
    //构造方法
    public Node(Object obj, Node nextval) {
        this.element = obj;
        this.next = nextval;
    }
    //获得当前结点的指针域
    public Node getNext() {
        return this.next;
    }
    //获得当前结点数据域的值
    public Object getElement() {
        return this.element;
    }
    //设置当前结点的指针域
    public void setNext(Node nextval) {
        this.next = nextval;
    }
    //设置当前结点数据域的值
    public void setElement(Object obj) {
        this.element = obj;
    }
    public String toString() {
        return this.element.toString();
    }
}
public interface ListForTest {
    //获得线性表长度
    public int size();
    //判断线性表是否为空
    public boolean isEmpty();
    //插入元素
    public void insert(int index, Object obj) throws Exception;
    //删除元素
    public void delete(int index) throws Exception;
    //获取指定位置的元素
    public Object get(int index) throws Exception;
}
public class TextList implements ListForTest{
    Node head; //头指针
    Node current;//当前指针
    int size;//数量
    //构造函数
    public TextList(){
        head = new Node(null,null);
        current = head;
        this.size = 0;
    }
    //当前索引函数
     public void index(int index) throws Exception
        {
            if(index <0 || index > size -1)
            {
              throw new Exception("参数错误!");    
            }
            int j=0;//循环变量
            while(current != null&&j<index)
            {
                current = current.next;
                j++;
            }
        }    
    public int size() {
        // TODO Auto-generated method stub
        return this.size;
    }
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return this.size == 0;
    }
    public void insert(int index, Object obj) throws Exception {
        // TODO Auto-generated method stub
        if(index <0 ||index >size)
        {
            throw new Exception("参数错误!");
        }
        if(index == 0){
            Node node = new Node(obj,head);
            head = node;
        }else{
             index(index-1);//定位到要操作结点的前一个结点对象。
                current.setNext(new Node(obj,current.next));
        }
        size++;
    }
    public void delete(int index) throws Exception {
        // TODO Auto-generated method stub
        //判断链表是否为空
        if(isEmpty())
        {
            throw new Exception("链表为空,无法删除!");
        }
        if(index <1 ||index >size)
        {
            throw new Exception("参数错误!");
        }
        if(index == 0){
            head = head.next;
        }else{
             index(index-1);//定位到要操作结点的前一个结点对象。
             current.setNext(current.next.next);
        }
        size--;
    }
    public Object get(int index) throws Exception {
        // TODO Auto-generated method stub
        if(index <0 || index >size-1)
        {
            throw new Exception("参数非法!");
        }
        index(index);
        return current.getElement();
    }
    public class Node {
        Object element; //数据域
        Node next;  //指针域
        //构造方法
        public Node(Object obj, Node nextval) {
            this.element = obj;
            this.next = nextval;
        }
        //获得当前结点的指针域
        public Node getNext() {
            return this.next;
        }
        //获得当前结点数据域的值
        public Object getElement() {
            return this.element;
        }
        //设置当前结点的指针域
        public void setNext(Node nextval) {
            this.next = nextval;
        }
        //设置当前结点数据域的值
        public void setElement(Object obj) {
            this.element = obj;
        }
        public String toString() {
            return this.element.toString();
        }
    }
}
在单链表的任何位置上插入数据元素的概率相等时,在单链表中插入一个数据元素时比较数据元素的平均次数为:

删除单链表的一个数据元素时比较数据元素的平均次数为:

因此,单链表插入和删除操作的时间复杂度均为O(n)。另外,单链表读取数据元素操作的时间复杂度也为O(n)。
优点:主要优点是支持随机读取,以及内存空间利用效率高;
缺点:主要缺点是需要预先给出数组的最大数据元素个数,而这通常很难准确作到。当实际的数据元素个数超过了预先给出的个数,会发生异常。另外,顺序表插入和删除操作时需要移动较多的数据元素。
优点:主要优点是不需要预先给出数据元素的最大个数。另外,单链表插入和删除操作时不需要移动数据元素;
缺点:主要缺点是每个结点中要有一个指针,因此单链表的空间利用率略低于顺序表的。另外,单链表不支持随机读取,单链表取数据元素操作的时间复杂度为O(n);而顺序表支持随机读取,顺序表取数据元素操作的时间复杂度为O(1)。
面试之路(10)-BAT面试之java实现单链表的插入和删除
原文:http://blog.csdn.net/lpjishu/article/details/51278140