【数据结构】单链表(基础)

学习记录,若有不足请指出来

链表

1 结点结构

// Definition for singly-linked list.
struct SinglyListNode {
    int val;
    SinglyListNode *next;
    SinglyListNode(int x) : val(x), next(NULL) {}
};

2 操作

与数组不同,我们无法在常量时间内访问单链表中的随机元素。 如果我们想要获得第 i 个元素,我们必须从头结点逐个遍历。 我们按索引来访问元素平均要花费O(N)时间,其中 N 是链表的长度。

添加操作-单链表

  1. 令插入结点的next为其插入位置下结点

  2. 原位置下一节点为要插入的结点即可

lianbiao1

与数组不同,我们不需要将所有元素移动到插入元素之后。因此,您可以在 O(1) 时间复杂度中将新结点插入到链表中,这非常高效。

删除操作-单链表

//删除prev的下一结点
prev->next == prev->next ->next;

删除第一个结点

SinglyListNode *deleteTopList = singlylistNode->next; //singlyListnode为需要删除头结点的链表
singlylistnode -> next = deletTopList;

3 设计链表

设计链表的实现。

​ 单链表中的节点属性:valnext(如果是双向链表,还需要属性prev以指示链表中的上一个节点)

在链表中需要实现的功能有:

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

代码

虽然看着很简单,但是还是要认真写才行

class MyLinkedList {
public:
    /** Initialize your data structure here. */
    struct LinkedNode {
        int val;
        LinkedNode* next;
        LinkedNode(int val):val(val),next(nullptr){}
        LinkedNode(int val,LinkedNode* next1):val(val),next(next1){}
    };
  
    MyLinkedList(){
        dummyHead = new LinkedNode(0);
        _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) {
        if(index > (_size - 1) || index < 0)
            return -1;
        LinkedNode* top = dummyHead->next;
        while(index--)
            top = top->next;
        return top->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) {
        LinkedNode *second = dummyHead->next;
        LinkedNode* addNode = new LinkedNode(val,second);
        dummyHead->next = addNode;
        _size++;
    }
    
    /** Append a node of value val to the last element of the linked list. */
    void addAtTail(int val) {
        LinkedNode* newNode=new LinkedNode(val);
        LinkedNode* tail = dummyHead;
        while(tail->next != nullptr){
            tail = tail->next;
        }
        tail -> 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 ) {
            return;
        }
        LinkedNode * indexNode = dummyHead;
        while(index--){
            indexNode = indexNode->next;
        }
        LinkedNode* tmp = new LinkedNode(val,indexNode->next);
        indexNode->next = tmp;
        _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;
        }
        LinkedNode* indexNode = dummyHead;
        while(index--){
            indexNode = indexNode->next;
        }
        LinkedNode* tmp = indexNode->next;
        indexNode->next = indexNode->next->next;
        delete tmp;
        _size--; 
    }
private:
    int _size;
    LinkedNode* dummyHead;
};

/**
 * 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);
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

欧恩意

如有帮助,感谢打赏!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值