其他分享
首页 > 其他分享> > 力扣 - 707. 设计链表

力扣 - 707. 设计链表

作者:互联网

目录

题目

707. 设计链表

思路1(单链表)

代码

class ListNode {
    int val;
    ListNode next;
    public ListNode(int val) {
        this.val = val;
    }
}

class MyLinkedList {
    int size;
    ListNode head;

    /** Initialize your data structure here. */
    public MyLinkedList() {
        head = new ListNode(-1);
        size = 0;
    }
    
    /** 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 (index < 0 || index >= size) {
            return -1;
        }
        ListNode cur = head;
        while (index >= 0) {
            cur = cur.next;
            index--;
        }
        return cur.val;
    }
    
    /** 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) {
        addAtIndex(0, val);
    }
    
    /** Append a node of value val to the last element of the linked list. */
    public void addAtTail(int val) {
        addAtIndex(size, val);
    }
    
    /** 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 (index < 0 || index > size) {
            return;
        }
        size++;
        ListNode pre = head;
        while (index > 0) {
            pre = pre.next;
            index--;
        }
        ListNode n = new ListNode(val);
        n.next = pre.next;
        pre.next = n;
    }
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    public void deleteAtIndex(int index) {
        if (index < 0 || index >= size) {
            return;
        }
        size--;
        ListNode pre = head;
        while (index > 0) {
            pre = pre.next;
            index--;
        }
        pre.next = pre.next.next;
    }
}

/**
 * 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);
 */

复杂度分析

思路2(双链表)

代码

class ListNode {
    int val;
    ListNode pre;
    ListNode next;

    public ListNode() {
    }

    public ListNode(int x) {
        val = x;
    }
}
class MyLinkedList {
    int size;
    ListNode head;
    ListNode tail;

    /** Initialize your data structure here. */
    public MyLinkedList() {
        head = new ListNode();
        tail = new ListNode();
        head.next = tail;
        tail.pre = head;
        size = 0;
    }
    
    /** 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 (index >= 0 && index < size) {
            ListNode p = head;
            while (index >= 0) {
                p = p.next;
                index--;
            }
            return p.val;
        }
        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) {
        ListNode n = new ListNode(val);
        n.next = head.next;
        n.pre = head;
        n.pre.next = n;
        n.next.pre = n;
        size++;
    }
    
    /** Append a node of value val to the last element of the linked list. */
    public void addAtTail(int val) {
        ListNode n = new ListNode(val);
        n.pre = tail.pre;
        n.next = tail;
        n.next.pre = n;
        n.pre.next = n;
        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 (index >= 0 && index < size + 1) {
            ListNode p = head;
            ListNode n = new ListNode(val);
            while (index > 0) {
                p = p.next;
                index--;
            }
            n.next = p.next;
            n.pre = p;
            n.next.pre = n;
            n.pre.next = n;
            size++;
        }
    }
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    public void deleteAtIndex(int index) {
        if (index >= 0 && index < size) {
            ListNode p = head;
            while (index > 0) {
                p = p.next;
                index--;
            }
            p.next = p.next.next;
            p.next.pre = p;
            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);
 */

复杂度分析

标签:pre,力扣,ListNode,val,index,int,707,next,链表
来源: https://www.cnblogs.com/linzedian/p/13944367.html