【Likou】707. Designing a Linked List<Linked List Pointer>

【Rich Button】707. Design linked list

[Likou] 707. Link to the original question of designing a linked list

Implement the MyLinkedList class:

  • MyLinkedList() Initializes the MyLinkedList object.
  • int get(int index) Get the value of the node whose subscript is index in the linked list. Returns -1 if the subscript is invalid.
  • void addAtHead(int val) Insert a node whose value is val before the first element in the linked list . After the insertion is complete, the new node becomes the first node of the linked list.
  • void addAtTail(int val) Add a node whose value is val to the linked list as the last element of the linked list.
  • void addAtIndex(int ​​index, int val) Insert a node whose value is val before the node whose subscript is index in the linked list. If index is equal to the length of the linked list, then the node will be appended to the end of the linked list. If index is greater than length, the node will not be inserted into the linked list.
  • void deleteAtIndex(int ​​index) If the subscript is valid, delete the node whose subscript is index in the linked list.

answer

//单链表
class ListNode {
    
    
    int val;
    ListNode next;
    ListNode(){
    
    }
    ListNode(int val) {
    
    
        this.val=val;
    }
}

class MyLinkedList {
    
    
    //链表元素的个数
    int size;
    //虚拟头结点
    ListNode dummyNode;

    //初始化链表
    public MyLinkedList() {
    
    
        size = 0;
        dummyNode = new ListNode(-1);
    }

    //获取第 index 个节点的数值,注意 index 是从0开始的,第 0 个节点就是头结点
    public int get(int index) {
    
    
        if (index < 0 || index >= size) {
    
    
            return -1;
        }

        ListNode cur = dummyNode;
        //包含一个虚拟头节点,所以查找第 index+1 个节点
        for (int i = 0; i <= index; i++) {
    
    
            cur = cur.next;
        }
        return cur.val;
    }

    //在链表最前面插入一个节点,等价于在第 0 个元素前添加
    public void addAtHead(int val) {
    
    
        addAtIndex(0, val);
    }

    //在链表的最后插入一个节点,等价于在(末尾+1)个元素前添加
    public void addAtTail(int val) {
    
    
        addAtIndex(size, val);
    }


    public void addAtIndex(int index, int val) {
    
    
        if (index > size) {
    
    
            return;
        }
        if (index < 0) {
    
    
            index = 0;
        }
        size++;

        //找到要插入节点的前驱
        ListNode cur = dummyNode;
        for (int i = 0; i < index; i++) {
    
    
            cur = cur.next;
        }
        //要插入的节点
        ListNode toAdd = new ListNode(val);
        toAdd.next = cur.next;
        cur.next = toAdd;
    }


    public void deleteAtIndex(int index) {
    
    
        if (index < 0 || index >= size) {
    
    
            return;
        }
        size--;
        ListNode cur = dummyNode;
        for (int i = 0; i < index ; i++) {
    
    
            cur = cur.next;
        }
        cur.next = cur.next.next;
    }
}
//双链表
class ListNode{
    
    
    int val;
    ListNode next,prev;
    ListNode() {
    
    };
    ListNode(int val){
    
    
        this.val = val;
    }
}

class MyLinkedList {
    
    
    //记录链表中元素的数量
    int size;
    //记录链表的虚拟头结点和尾结点
    ListNode dummyNode,tail;

    public MyLinkedList() {
    
    
        //初始化操作
        this.size = 0;
        this.dummyNode = new ListNode(-1);
        this.tail = new ListNode(-1);
        //这一步非常关键,否则在加入头结点的操作中会出现null.next的错误!!!
        dummyNode.next=tail;
        tail.prev=dummyNode;
    }

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

    public void addAtHead(int val) {
    
    
        //等价于在第0个元素前添加
        addAtIndex(0,val);
    }

    public void addAtTail(int val) {
    
    
        //等价于在最后一个元素(null)前添加
        addAtIndex(size,val);
    }

    public void addAtIndex(int index, int val) {
    
    
        //index大于链表长度
        if(index>size){
    
    
            return;
        }
        //index小于0
        if(index<0){
    
    
            index = 0;
        }
        size++;
        //找到前驱
        ListNode pre = this.dummyNode;
        for(int i=0; i<index; i++){
    
    
            pre = pre.next;
        }
        //新建结点
        ListNode newNode = new ListNode(val);
        newNode.next = pre.next;
        pre.next.prev = newNode;
        newNode.prev = pre;
        pre.next = newNode;

    }

    public void deleteAtIndex(int index) {
    
    
        //判断索引是否有效
        if(index<0 || index>=size){
    
    
            return;
        }
        //删除操作
        size--;
        ListNode pre = this.dummyNode;
        for(int i=0; i<index; i++){
    
    
            pre = pre.next;
        }
        pre.next.next.prev = pre;
        pre.next = pre.next.next;
    }
}

Guess you like

Origin blog.csdn.net/qq_44033208/article/details/132494148