链表实现(用伪结点)

这篇博客详细介绍了如何使用Java实现单链表和双链表,包括初始化、获取节点值、添加节点、插入节点和删除节点等操作。双链表通过判断插入位置靠近头部还是尾部,提高了效率。文章还强调了在实现时定义前一个节点和后一个节点的重要性,简化了插入和删除操作。
摘要由CSDN通过智能技术生成

leetcode707

单链表实现

//先定义结点node
    class Node {
        int data;
        Node next;
        Node(){}
        Node(int data){this.data=data;}
        Node(int data,Node next){this.data=data;this.next=next;}
    }
    class MyLinkedList {
        Node head;
        int length;

        /** Initialize your data structure here. */
        public MyLinkedList() {
            this.head=new Node(-1);
            this.length=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(length==0) return -1;
            if(index>=length||index<0) return -1;
            Node cur=head.next;
            while(index>0){
                cur=cur.next;
                index--;
            }
            return cur.data;
        }

        /** 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) {
            Node newNode =new Node(val);
            newNode.next=head.next;
            head.next=newNode;
            length++;
        }

        /** Append a node of value val to the last element of the linked list. */
        public void addAtTail(int val) {
            Node cur=head.next;
            if(cur!=null){
                while (cur.next!=null) {
                    cur=cur.next;
                }
                Node newNode=new Node(val);
                cur.next=newNode;
                length++;
            }else{
                Node newNode=new Node(val);
                head.next=newNode;
                length++;
            }

        }

        /** 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>length) return;
            if(index<0) addAtHead(val);
            if(index==length) addAtTail(val);
            else {
                Node newNode=new Node(val);
                Node cur =head;
                while(index>0){
                    cur=cur.next;
                    index--;
                }
                newNode.next=cur.next;
                cur.next=newNode;
                length++;
            }
        }

        /** Delete the index-th node in the linked list, if the index is valid. */
        public void deleteAtIndex(int index) {
            if(index<0 || index>=length) return;
            Node cur=head;
            while(index>0){
                cur=cur.next;
                index--;
            }
            cur.next=cur.next.next;
            length--;
        }
    }

里面length==0还可以写的更简洁一点,懒得改了,后面双链表修改 

双链表(两个伪结点,一头一尾)

class Node{
        int data;
        Node prev;
        Node next;
        public Node(int data){
            this.data=data;
        }
    }
    class MyLinkedList {
        int size;
        Node head;
        Node tail;

        /** Initialize your data structure here. */
        public MyLinkedList(){
            this.size=0;
            this.head=new Node(0);
            this.tail=new Node(0);
            this.head.next=this.tail;
            this.tail.prev=this.head;
        }


        /** 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;
            Node cur=null;
            if(index+1<size-index){    //near head
                cur=head;
                for(int i=0;i<index+1;i++){
                    cur= cur.next;
                }
            }else {                   //near tail
                cur=tail;
                for(int i=0;i<size-index;i++){
                    cur=cur.prev;
                }
            }
            return cur.data;
        }

        /** 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) {
            Node newNode=new Node(val);
            Node pred=head;
            Node succ=head.next;

            newNode.next=succ;
            newNode.prev=pred;
            pred.next=newNode;
            succ.prev=newNode;
            size++;
        }

        /** Append a node of value val to the last element of the linked list. */
        public void addAtTail(int val) {
            Node newNode=new Node(val);
            Node pred=tail.prev;
            Node succ=tail;

            newNode.next=succ;
            newNode.prev=pred;
            pred.next=newNode;
            succ.prev=newNode;
            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>size) return;
            else if(index<0) {
                addAtHead(val);
                return;
            }else {
                Node cur=null;
                Node newNode=new Node(val);
                if(index+1<size-index){   //near head
                    cur=head;
                    for(int i=0;i<index+1;i++){
                        cur=cur.next;
                    }
                }else {                  //near tail
                    cur=tail;
                    for(int i=0;i<size-index;i++){
                        cur=cur.prev;
                    }
                }
                //在cur之前插入
                Node pred=cur.prev;
                Node succ=cur;
                newNode.prev=pred;
                newNode.next=succ;
                pred.next=newNode;
                succ.prev=newNode;
                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) return;
            Node cur=null;
            if(index+1<size-index){   //near head
                cur=head;
                for(int i=0;i<index+1;i++){
                    cur=cur.next;
                }
            }else {                  //near tail
                cur=tail;
                for(int i=0;i<size-index;i++){
                    cur=cur.prev;
                }
            }
            //delete cur

            cur.prev.next=cur.next;
            cur.next.prev=cur.prev;
            size--;

        }
    }

 

 双链表更快,因为会判断index靠近头还是靠近尾,省了一半的时间,注意实现的时候,实现定义好前一个结点pred,和后一个结点succ,这样能简化插入,删除操作。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值