/**
 * Java实现单链表
 */
public class SingleLinkedList {

    private Node head;
    private int size;

    public SingleLinkedList() {
        size = 0;
        head = null;
    }

    public static void main(String[] args) {
        SingleLinkedList linkedList = new SingleLinkedList();
        linkedList.addNodeHead(34);
        linkedList.addNodeHead(67);
        System.out.println(linkedList);
    }

    /**
     * 在链表头部插入(不用遍历链表,如果在尾部需要遍历)
     *
     * @param value
     */
    public void addNodeHead(Object value) {

        Node newNode = new Node(value);
        if (size == 0) {
            head = newNode;
        } else {
            newNode.next = head;
            head = newNode;
        }
        size++;
    }

    /**
     * 在链表尾部插入(不用遍历链表,如果在尾部需要遍历)
     *
     * @param value
     */
    public void addNodeTail(Object value) {
        Node temp = head;
        while (temp.next != null) {
            temp = head.next;
        }
        temp.next = new Node(value);
        size++;
    }

    /**
     * 删除链表元素
     */
    public void deleteNode() {
        head = head.next;
        size--;
    }

    /**
     * 根据值查找某个节点
     *
     * @param value
     * @return
     */
    public Node find(Object value) {
        Node current = head;
        int count = 0;
        while (count < size) {
            if (current.value.equals(value)) {
                return current;
            } else {
                current = current.next;
            }
            count++;
        }
        return null;
    }

    /**
     * 根据位置查找某个节点
     *
     * @param index
     * @return
     */
    public Node find(int index) {
        Node current = head;
        int count = 0;
        while (index < size) {
            if (count == index) {
                return current;
            } else {
                current = current.next;
            }
            count++;
        }
        return null;
    }

    /**
     * 删除某个位置的节点
     *
     * @param index
     */
    public void delete(int index) {
        if (index < 0 || index > size) {
            return;
        }
        Node temp = head;
        int count = 0;
        while (temp.next != null) {
            if (index == count) {
                temp.next = temp.next.next;
                return;
            }
            count++;
            temp = temp.next;
        }
    }


    /**
     * 在某个位置插入节点
     *
     * @param index
     * @param value
     */
    public void insert(int index, Object value) {
        if (index < 0 || index > size) {
            return;
        }
        Node newNode = new Node(value);
        Node temp = head;
        int count = 0;
        while (temp.next != null) {
            if (index == count) {
                newNode.next = temp.next.next;
                return;
            }
            count++;
            temp = temp.next;
        }
        size++;
    }

    /**
     * 迭代反转单链表
     *
     * @param node
     * @return
     */
    public Node reverse(Node node) {
        Node prev = null;
        Node now = node;
        while (now != null) {
            Node next = node.next;
            now.next = prev;
            prev = now;
            now = next;
        }
        return prev;
    }

    private class Node {
        private Object value; // 不是方法的变量是类的变量,所以在这可以用private修饰
        private Node next;

        private Node(Object value) {
            this.value = value;
        }
    }
}

 

11-27 00:41