LeetCode 707. 设计链表

题目描述

设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性: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

提示

所有val值都在 [1, 1000] 之内。
操作次数将在  [1, 1000] 之内。
请不要使用内置的 LinkedList 库。

思路

首先是定义了一个数据结构MyListNode。

struct MyListNode {
    int val;
    MyListNode* next;
    MyListNode(int x) : val(x), next(nullptr) {}
};

在类里定义size和head,构造函数也是初始化head和size。
然后就是get函数了。获取第index个元素,具体就是首先定义一个ans来保存答案。判断index有没有越界,如果有返回-1,如果没有就是一直到index–结束为止ans=ans->next,最后返回ans->val即可。
头插法就是新建一个ListNode再把head->next给新建的ListNode的next,再把新建的节点的地址给head->next就可以了。记得size++哟(菜鸡的我在尾插时忘了…)。
尾插就是倒过来的,跑到链表最后再加上新建的ListNode就可以了。
在制定位置插入值。这我用的是哨兵法,刚好复习一下昨天刷过的内容,先创建哨兵初始化为0,然后一个前继一个后驱,不过要注意的是要这么加的话不能只遍历到index就结束要多遍历一下然后在当前节点和前继节点中添加元素即可。
最后是删除某个位置的元素,也是跟上面的一样,用哨兵法解决的,找到要删除的元素后把前继节点和后驱连在一起就可以了

代码

说了这么多上代码:

struct MyListNode {
    int val;
    MyListNode* next;
    MyListNode(int x) : val(x), next(nullptr) {}
};
class MyLinkedList {
private:
    MyListNode* head;
    int size;
public:

    /** Initialize your data structure here. */
    MyLinkedList() {
        this->head = new MyListNode(0);
        this->size = 0;
    }
    
    /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
    int get(int index) {
        MyListNode* ans = head->next;
        if (index > size - 1 || index < 0) return -1;
        while (index--) {
            ans = ans->next;
        }
        return ans->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. */
    void addAtHead(int val) {
        MyListNode* newNode = new MyListNode(val);
        newNode->next = head->next;
        head->next = newNode;
        size++;
    }
    
    /** Append a node of value val to the last element of the linked list. */
    void addAtTail(int val) {
        MyListNode* newNode = new MyListNode(val);
        MyListNode* curr = head;
        while (curr->next) {
            curr = curr->next;
        }
        curr->next = 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. */
    void addAtIndex(int index, int val) {
        if (index > size || index < 0) return;
        int tar = index + 1;
        MyListNode* sen = new MyListNode(0);

        sen->next = head;

        MyListNode* add = new MyListNode(val);
        MyListNode* cur = head, *prev = sen;

        while (tar--) {
            prev = cur;
            cur = cur->next;
        }
        prev->next = add;
        add->next = cur;
        size++;
    }
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    void deleteAtIndex(int index) {
        if (index >= size || index < 0) return ;
        
        int target = index + 1;
        MyListNode* sen = new MyListNode(0);
        sen->next = head;
        MyListNode* curr = head, *prev = sen, *toDelete = nullptr;

        while (target--) {
            prev = curr;
            curr = curr->next;
        }
        prev->next = curr->next;
        toDelete = curr;
        delete toDelete;
        toDelete = nullptr;
        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);
 */
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值