链表专题1.设计链表

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

1单向链,有头指针无尾指针表实现
代码实现:

```cpp
class MyLinkedList {
public:
    struct ListNode
    {
     int val;
    ListNode *next,*prev;
    ListNode(int x) : val(x), next(NULL) {}
    };
    ListNode *head;
    /** Initialize your data structure here. */
    MyLinkedList(){
        head = nullptr;
    }
    int length(){//获取当前长度
        ListNode *q=head;
        int i;
        while(q!=NULL){
            q=q->next;
            i++;//i为链表长度
        }
        return i;
    }
    /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
    //获取链表中第 index 个节点的值。如果索引无效,则返回-1。
    int get(int index) {
        int i=0;
        ListNode *p=head;
        while(i<index && p!=NULL){//防止p==NULL的情况出现
            p=p->next;//
            i++;
        }
        if(p!=NULL){
            return p->val;
        }
        else{
            return -1;
        }
    }
    
    /** 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. */
    //在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
    void addAtHead(int val) {
        ListNode *p=new ListNode(val);//创建一个节点P其值为val
        p->next=head;//将p的next指针指向head
        head=p;//更换头节点
    }
    
    /** Append a node of value val to the last element of the linked list. */
    //将值为 val 的节点追加到链表的最后一个元素。
    void addAtTail(int val) {
        ListNode *p=new ListNode(val);//创建一个节点P其值为val
        ListNode *q=head;
        if(head==NULL){//链表为空的情况下
            head=p;
        }
        while(q!=NULL){//找到最后一个空节点
            q=q->next;
        }
        p->prev=q->prev;
        q->prev->next=p;//将q前元素的next指针指向p
        q->prev=p;//将q前指针指向p
        p->next=q;
    }
    
    /** 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. */
    //addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val  的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
    void addAtIndex(int index, int val) {
        ListNode *p=new ListNode(val);//创建一个节点P其值为val
        ListNode *q=head;
        int i=length();;//因为要在第index个节点之前增加
        if(index>i){
            return;
        }
        else if(index==i){//该节点将附加到链表的末尾
            if(head==NULL){//如果为空表
                head=p;
            }
            else{
            ListNode *a=head;
            while(a->next!=NULL){
            a=a->next;
            }
            p->next=NULL;
            a->next=p;
            }
        }
        else if(index<i && index>0){//在第index前插入
           int j=0;
            ListNode *b=head;
            while(b!=NULL && j<index-1){
                b=b->next;
                j++;
            }//此时b已经到了index前的节点
            p->next=b->next;
            b->next=p;
        }
        else if(index<=0){//在头部插入节点
            p->next=head;
            head=p;
        }
    }
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    //如果索引 index 有效,则删除链表中的第 index 个节点。
    void deleteAtIndex(int index) {
        int i=0;
        ListNode *q=head;
        if(index==0 && head!=NULL){
            ListNode *p=head;
            head=head->next;
            delete p;
            return;
        }
        while(q!=NULL && i<index-1){//找到其前一个节点
            q=q->next;
            i++;
        }
        if(q!=NULL && q->next!=NULL){//q->next实际上就是要删除的节点
            q->next=q->next->next;
        }
    }
};

/**
 * 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);
 */


2.双向链有头指针,无尾指针表实现
代码实现:

```cpp
class MyLinkedList {
public:
    struct ListNode
    {
     int val;
    ListNode *next,*prev;
    ListNode(int x) : val(x), next(NULL) {}
    };
    ListNode *head;
    /** Initialize your data structure here. */
    MyLinkedList(){
        head = nullptr;
    }
    int length(){//获取当前长度
        ListNode *q=head;
        int i;
        while(q!=NULL){
            q=q->next;
            i++;//i为链表长度
        }
        return i;
    }
    /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
    //获取链表中第 index 个节点的值。如果索引无效,则返回-1。
    int get(int index) {
        int i=0;
        ListNode *p=head;
        while(i<index && p!=NULL){//防止p==NULL的情况出现
            p=p->next;//
            i++;
        }
        if(p!=NULL){
            return p->val;
        }
        else{
            return -1;
        }
    }
    
    /** 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. */
    //在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
    void addAtHead(int val) {
        ListNode *p=new ListNode(val);//创建一个节点P其值为val
        p->next=head;//将p的next指针指向head
        head=p;//更换头节点
    }
    
    /** Append a node of value val to the last element of the linked list. */
    //将值为 val 的节点追加到链表的最后一个元素。
    void addAtTail(int val) {
        ListNode *p=new ListNode(val);//创建一个节点P其值为val
        ListNode *q=head;
        if(head==NULL){//链表为空的情况下
            head=p;
        }
        while(q->next!=NULL){//找到最后一个节点
            q=q->next;
        }
        p->next=q->next;//将p的后驱指向q的后驱
        q->next=p;//使q的后驱指向p
        p->prev=q;//p的前驱指向q
    }
    
    /** 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. */
    //addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val  的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
    void addAtIndex(int index, int val) {
        ListNode *p=new ListNode(val);//创建一个节点P其值为val
        if(index>length()){
            return;
        }
        else if(index==length()){//该节点将附加到链表的末尾
            if(head==NULL){//如果为空表
                head=p;
            }
            else{
            ListNode *a=head;
            while(a->next!=NULL){
            a=a->next;
            }
            p->next=a->next;//将p的后驱指向q的后驱
            a->next=p;//使q的后驱指向p
            p->prev=a;//p的前驱指向q
            }
        }
        else if(index<length() && index>0){//在第index前插入
            int j=0;
            ListNode *b=head;
            while(b!=NULL && j<index-1){
                b=b->next;
                j++;
            }//此时b已经到了index前的节点
            p->next=b->next;
            if(b->next!=NULL){
            b->next->prev=p;}
            p->prev=b;
            b->next=p;
        }
        else if(index<=0){//在头部插入节点
            p->next=head;
            if(head!=NULL){
            head->prev=p;}
            head=p;
        }
    }
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    //如果索引 index 有效,则删除链表中的第 index 个节点。
    void deleteAtIndex(int index) {
        int i=0;
        ListNode *q=head;
        if(index==0 && head!=NULL){
            head=head->next;
            return;
        }
        while(q!=NULL && i<index-1){//找到其前一个节点
            q=q->next;
            i++;
        }
        if(q!=NULL && q->next!=NULL){//q->next实际上就是要删除的节点
            q->next=q->next->next;
        }
    }
};

/**
 * 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.进行b->next->prev=p时要先判断b->next是否为NULL
 if(b->next!=NULL){
 b->next->prev=p;}
 2.在进行移动时,不能移动到NULL因为这样已经无法对节点进行操作
 3.如果一个指针已经为空,比如说p==NULL,但是其判断条件中有p->next就会产生错误***
发布了35 篇原创文章 · 获赞 27 · 访问量 496

猜你喜欢

转载自blog.csdn.net/weixin_45221477/article/details/104909098