Leetcode链表(basic(1))

参考一些大佬的博客,多次测试终于写出来~
(下面附上完整cpp代码大佬勿喷
在这里插入图片描述

单链表的增删改查

https://leetcode-cn.com/explore/learn/card/linked-list/193/singly-linked-list/741/

#include <iostream>
using namespace std;

class ListNode{
public:
    int val;
    ListNode* next;
    ListNode(int x):val(x),next(NULL){};
};
class MyLinkedList {
public:
    ListNode *head=NULL;//定义头结点
    /** Initialize your data structure here. */
    MyLinkedList() {
        head=new ListNode(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(head==NULL||head->next == NULL)
            return -1;

        ListNode *p = head->next;
        
        int i = 0;
        while(p!=NULL){
            if(i==index){
                return p->val;
            }
            p = p->next;
            ++i;
        }
        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. */
    void addAtHead(int val) {
        ListNode *q = new ListNode(val);
        q->next = head->next;
        head->next = q;
    }
    
    /** Append a node of value val to the last element of the linked list. */
    void addAtTail(int val) {
        ListNode *p = head;
        while(p->next!=NULL){
            p = p->next;
        }
        ListNode *q = new ListNode(val);
        q->next = p->next;
        p->next = q;
    }
    void print(){
        ListNode *p = head->next;
        while(p!=NULL){
            cout<<p->val<<" ";
            p = p->next;
        }
    }
    /** 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<=0){
            addAtHead(val);
            return;
        }
        ListNode *p = head;
        int i = 0;
        while(p->next!=NULL){
            if(i==index){
                ListNode *node = new ListNode(val);
                node->next = p->next;
                p->next = node;
            }
            p = p->next;
            ++i;
        }
        //如果是最后一个节点
        if(i==index){
            ListNode *node = new ListNode(val);
             node->next = p->next;
            p->next = node;
        }
    }
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    void deleteAtIndex(int index) {
        ListNode *p = head;
        int i = 0;
        while(p->next!=NULL){
           if(i==index){
               p->next = p->next->next;
               return;
           }
           p = p->next;
           ++i;
        }
    }
};

/**
 * 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);
 */
int main(void){
    MyLinkedList* obj = new MyLinkedList();
    int index = 1;
    int val = 10;
    obj->addAtHead(val);
    obj->addAtTail(val);
    int param_1 = obj->get(index);
    cout<<param_1<<endl;
    obj->addAtIndex(2,20);
    obj->print();
    cout<<endl;
    obj->deleteAtIndex(2);
    obj->print();
    return 0;
}

双链表的增删改查

https://leetcode-cn.com/explore/learn/card/linked-list/196/doubly-linked-list/759/
双链表比单链表增加了尾指针和size,所以在进行addAtTail和addAtIndex以及delete操作时便于快速定位到所要寻找的节点

#include <iostream>
using namespace std;

struct ListNode
{
    int val;
    ListNode *pre;
    ListNode *next;
    ListNode(int x):val(x),pre(NULL),next(NULL){};
};

class MyLinkedList {
public:
//设置头尾两个哨兵节点
    ListNode *head;
    ListNode *tail;
    int size = 0;
    /** Initialize your data structure here. */
    MyLinkedList() {
        head=nullptr;
        tail = nullptr;
        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(head==NULL||head->next == NULL)
            return -1;

        ListNode *p = head;
        
        int i = 0;
        while(p!=NULL){
            if(i==index){
                return p->val;
            }
            p = p->next;
            ++i;
        }
        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. */
    void addAtHead(int val) {
        if(head!=nullptr){
            ListNode *q = new ListNode(val);
            q->next = head;
            head->pre = q;
            head = q;
        }else{
            head = new ListNode(val);
            tail = head;
        }
        size++;
        
    }
    
    /** Append a node of value val to the last element of the linked list. */
    void addAtTail(int val) {
        if(tail!=nullptr){
            ListNode *q = new ListNode(val);
            tail->next = q;
            q->pre = tail;
            tail = q;
        }else{
            //如果尾节点为空,则头结点也为空
            tail = new ListNode(val);
            head = tail;
        }
        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<=0){
            addAtHead(val);
            return;
        }
        //如果是最后一个节点
        if(size==index){
            addAtTail(val);
            return;
        }
        if(index>size)
            return;
        ListNode *p = head;
        int i = 0;
        while(p->next!=NULL){
            if(i==index){
                ListNode *node = new ListNode(val);
                node->next = p;
                node->pre = p->pre;
                p->pre->next = node;
                p->pre = node;
                size++;
                break;
            }
            p = p->next;
            ++i;
        }
    }
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    void deleteAtIndex(int index) {
        //链表为空,不能删除
        if(!head)
            return;
        ListNode *p = head;
        int i = 0;
        if(index==0){
            if(p->next==nullptr){
                tail = nullptr;//将尾部置空
                
            }else{
                head = head->next;
                head->pre= nullptr;
            }
            delete p;
            size--; 
            return;
        }
        if(index==size-1){
            p = tail;
            tail = tail->pre;
            if(tail){
                tail->next = nullptr;
            }
            delete p;
            size--;
            return;
        }
        while(p->next!=NULL){
            if(i==index){
                p->pre->next = p->next;
                p->next->pre = p->pre;
                delete p;
                size--;
                return;
            }
            p = p->next;
            ++i;
        } 
        
    }
};
/**
 * 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);
 */
int main(void){
    MyLinkedList obj;
    obj.addAtHead(10);
    obj.addAtTail(20);
   obj.addAtHead(30);
   obj.addAtHead(40);
   obj.addAtIndex(1,3);
    for(int i = 0; i<obj.size;++i){
        cout<<obj.get(i)<<" ";
    }
    cout<<endl;
    obj.deleteAtIndex(2);
    for(int i = 0; i<obj.size;++i){
        cout<<obj.get(i)<<" ";
    }
    return 0; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值