LeetCode707.设计链表(这道题有意思)

LeetCode707.设计链表(这道题有意思)

1. 题目链接

https://leetcode-cn.com/problems/design-linked-list/

2. 代码(注释有分析)

class ListNode{
    private int val;
    private ListNode next;

    public ListNode(){
        this.val = 0;
        this.next = null;
    }

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

    public int getVal(){
        return this.val;
    }

    public void setVal(int val){
        this.val = val;
    }

    public ListNode getNext(){
        return this.next;
    }

    public void setNext(ListNode node){
        this.next = node;
    }
}

class MyLinkedList {
    private ListNode head;
    private ListNode tail;
    private int length;

    /** Initialize your data structure here. */
    public MyLinkedList() {
        this.head = null;
        this.tail = null;
        this.length = 0;
    }

    /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
    public MyLinkedList(int val){
        this.head = new ListNode(val);
        this.tail = this.head;
        this.length = 1;
    }



    public int get(int index) {
        //如果超出范围直接返回-1
        if(index < 0 || index >= this.length) return -1;
        else{
            ListNode cur = this.head;
            for(int i =0 ;i < index; i++){
                cur = cur.getNext();
            }
            return cur.getVal();
        }
    }

    /** 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 node = new ListNode(val);
        if(this.length == 0){
            this.head = node;
            this.tail = node;
        }else{
            node.setNext(this.head);
            this.head = node;
        }
        this.length++;
    }

    /** Append a node of value val to the last element of the linked list. */
    public void addAtTail(int val) {
        ListNode node = new ListNode(val);
        if(this.length == 0) {
            this.head = node;
            this.tail = node;
        }else {
            this.tail.setNext(node);
            this.tail = node;
        }
        this.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) {
        //index超出链表长度,什么都不做
        if(index > this.length){
            return;
        }
        //index小于等于0,加在head
        else if(index <= 0){
            addAtHead(val);
        }
        //index为length是在链表末尾追加
        else if(index == this.length){
            addAtTail(val);
        }
        //index的范围是(0, length)
        else {
            ListNode node = new ListNode(val);
            ListNode cur = this.head;
            for(int i = 0; i < index - 1; i++){
                cur = cur.getNext();
            }
            //比如现有0号和1号节点,插入的节点是node,index是1号
            // 那么cur就是0号,cur.getNext()就是旧1号,将node节点的next指向旧1号
            node.setNext(cur.getNext());
            //再将0号节点cur的next指向新1号的node节点
            cur.setNext(node);
            this.length++;
        }
    }


    /** Delete the index-th node in the linked list, if the index is valid. */
    public void deleteAtIndex(int index) {
        //索引无效,什么都不做
        if(index < 0 || index >= this.length) return;

        //删掉的是头结点
        if(index == 0) {
            this.head = this.head.getNext();
        }
        //删掉的节点的范围是[1, length)
        else {
            ListNode cur = this.head;
            for(int i = 0; i < index - 1; i++) {
                cur = cur.getNext();
            }
            cur.setNext(cur.getNext().getNext());
            //如果要删除的节点是最后一个,记得要更新tail值
            if(index == this.length - 1){
                this.tail = cur;
            }
        }
        this.length--;
    }
}

/**
 * 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);
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值