算法题第9题-----设计链表,难度(中等)

题目

设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。

在链表类中实现这些功能:

get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。

示例

MyLinkedList linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1,2);   //链表变为1-> 2-> 3
linkedList.get(1);            //返回2
linkedList.deleteAtIndex(1);  //现在链表是1-> 3
linkedList.get(1);            //返回3

题解(代码简单思路书写,有待优化)

class MyLinkedList {

    private class Node{
        public Integer index;
        public  Node next;

        public Node(int index){
            this.index=index;
            this.next=null;
        }
        public Node(){
            this.index=null;
            this.next=null;
        }
    }

    private Node dumyHead;//定义一个虚拟头节点
   private int size=0;//记录链表长度

    /** Initialize your data structure here. */
    public MyLinkedList() {
        this.dumyHead=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(index>=size||index<0)
            return -1;
        return getNodeByIndex(index).index;
    }

    private Node getNodeByIndex(int index){

        Node target=dumyHead;
        for(int i=0;i<index+1;i++){
            target=target.next;
        }
        return target;
    }

    /** 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(dumyHead.next==null){
            dumyHead.next=new Node(val);
        }else{
            Node head=new Node(val);
            head.next=dumyHead.next;
            dumyHead.next=head;
        }
        size++;

    }

    /** Append a node of value val to the last element of the linked list. */
    public void addAtTail(int val) {
        addAtTail(dumyHead, val);
        size++;
    }

    private void addAtTail(Node node,int val) {
        if(node.next==null){
            node.next=new Node(val);
            return;
        }
            addAtTail(node.next,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){
            //在头节点插入
            Node node=new Node(val);
            addAtHead(val);
            size++;
        }else if(index>size){
            return;
        }else{
            Node prev=getNodeByIndex(index-1);
            Node temp=getNodeByIndex(index);
            if(temp==null){//尾节点添加
                addAtTail(val);
            }else{
                Node node=new Node(val);
                node.next=temp;
                prev.next=node;
                size++;
            }
        }
    }

    @Override
    public String toString(){
        StringBuilder sb=new StringBuilder();
        Node target=dumyHead.next;
        while(target!=null){
            sb.append(target.index);
            target=target.next;
            if(target!=null){
                sb.append("->");
            }
        }
        return sb.toString();
    }
    /** 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;
        }

        if(index==0){//删除头节点得特殊处理
            Node node=getNodeByIndex(0);
            dumyHead.next=node.next;
            node.next=null;
        }else{
            Node prev= getNodeByIndex(index-1);
            Node temp= getNodeByIndex(index);
            prev.next=temp.next;
            temp.next=null;

        }
        size--;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_38941937/article/details/103115255