Skip to content

Latest commit

 

History

History
126 lines (115 loc) · 3.15 KB

5001-实现链表结构.md

File metadata and controls

126 lines (115 loc) · 3.15 KB

思路:当前节点Node 存放下一个节点next。

class MyLinkedList {

private Node node;
public int size = 0;

/**
 * Initialize your data structure here.
 */
public MyLinkedList() {
    this.node = new Node();
}

/**
 * Get the value of the index-th node in the linked list. If the index is invalid, return -1.
 */
public int get(int index) {
    if (size<=index || index<0) {
        return -1;
        //throw new ArrayIndexOutOfBoundsException();
    }
    Node temp = this.node;
    for (int i = 0; i < size; i++) {
        if (i == index) {
            return temp.val;
        }
        temp = temp.next;
    }
    return -1;
}

    /**
     * Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
     */
    public void addAtHead(int val) {
        if (size == 0) {
            this.node.val = val;
        } else {
            Node headNode = new Node(val);
            headNode.next = this.node;
            this.node = headNode;
        }
        size++;
    }

    /**
     * Append a node of value val to the last element of the linked list.
     */
    public void addAtTail(int val) {
        if (size == 0) {
            this.node.val = val;
        } else {
            Node lastNode = new Node(val);
            Node temp = this.node;
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = lastNode;
        }
        size++;
    }

    /**
     * Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
     */
    public void addAtIndex(int index, int val) {
        if (size < index) {//角标越界
            return;
            //throw new ArrayIndexOutOfBoundsException();
        }
        Node newNode = new Node(val);
        if (index == 0) {
            addAtHead(val);
            return;
        }
        Node temp = this.node;
        for (int i = 1; i <= size; i++) {
            if (i == index) {
                newNode.next = temp.next;
                temp.next = newNode;
            }
            temp = temp.next;
        }
        size++;
    }

    /**
     * Delete the index-th node in the linked list, if the index is valid.
     */
    public void deleteAtIndex(int index) {
        if (size < index) {
            return;
            //throw new ArrayIndexOutOfBoundsException();
        }
        if (index == 0) {
            Node next = this.node.next;
            this.node = next;
            return;
        }
        Node temp = this.node;
        for (int i = 1; i <= size; i++) {
            if(temp.next==null)return;
            if (i == index) {
                temp.next = temp.next.next;
                break;
            }
            temp = temp.next;
        }
        size--;
    }

    public class Node {
        public int val;//当前节点的值
        public Node next;//下一个节点的数据

        public Node() {
        }

        public Node(int val) {
            this.val = val;
        }
    }
}