LeetCode:707. 设计链表

力扣题目链接

单链表

class ListNode {
    int val;
    ListNode next;
    public ListNode(int val) {
        this.val = val;
    }
}
class MyLinkedList {
    int size;
    ListNode head;
    public MyLinkedList() {
        size = 0;
        head = new ListNode(0);
    }
    public int get (int index) {
        if ( index < 0 || index >= size) return -1;
        ListNode temp = head;
        for (int i = 0; i <= index; i++) {
            temp = temp.next;
        }
        return temp.val;
    }
    public void addAtHead(int val) {
        addAtIndex(0,val);
    }
    public void addAtTail(int val) {
        addAtIndex(size,val);

    }
    public void addAtIndex(int index, int val) {
        if (index < 0 ||index > size) return;
        ListNode temp = head;
        for (int i = 0; i < index; i++) {
            temp = temp.next;
        }
        ListNode p = new ListNode(val);
        p.next = temp.next;
        temp.next = p;
        size ++;
    }
    public void deletAtIndex(int index) {
        if (index < 0 || index >= size) return;
        ListNode temp = head;
        for (int i = 0; i < index; i++) {
            temp = temp.next;
        }
        temp.next = temp.next.next;
        size --;
    }
}

双链表

class ListNode {
    int val;
    ListNode next;
    ListNode prev;
    public ListNode(int val) {
        this.val = val;
    }
}
class MyLinkedList {
    int size;
    ListNode head,tail;
    public MyLinkedList() {
        size = 0;
        head = new ListNode(0);
        tail = new ListNode(0);
        head.next = tail;
        tail.prev = head;
    }

    public int get(int index) {
        //判断index是否有效
        if (index < 0 || index >= size) return -1;
        ListNode temp = head;
        //判断哪边遍历时间最短
        if (index >= size / 2) {
            //从tail开始;
            temp = tail;
            for (int i = 0; i < size - index; i ++) {
                temp = temp.prev;
            }
        }else {
            //从head开始:
            for (int i = 0; i <= index; i++) {
                temp = temp.next;
            }
        }
        return temp.val;
    }

    public void addAtHead(int val) {
        addAtIndex(0,val);
    }

    public void addAtTail(int val) {
        addAtIndex(size,val);

    }

    public void addAtIndex(int index, int val) {
        if (index < 0 || index > size) return;
        ListNode temp = head;
        for (int i = 0; i < index; i ++) {
            temp = temp.next;
        }
        ListNode p = new ListNode(val);
        p.next = temp.next;
        temp.next.prev = p;
        temp.next = p;
        p.prev = temp;
        size ++;
    }

    public void deleteAtIndex(int index) {
        if (index < 0 || index >= size) return;
        ListNode temp = head;
        for (int i = 0; i <= index; i++) {
            temp = temp.next;
        }
        temp.prev.next = temp.next;
        temp.next.prev = temp.prev;
        size --;
    }
}


/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList obj = new MyLinkedList();
 * int param_1 = obj.get(index);
 * obj.addAtHead(val);
 * obj.addAtTail(val);
 * obj.addAtIndex(index,val);
 * obj.deleteAtIndex(index);
 */

你可能感兴趣的:(leetcode,链表,算法)