LeetCode--链表1-单链表

LeetCode--链表1-单链表

单链表模板

  1. 初始化
  2. 头部插入
  3. 尾部插入
  4. 删除节点
  5. Index插入
  6. Index返回对应的节点指针和val值
class MyLinkedList {

private:
    // 定义单链表的节点
    struct ListNode
    {
        int val;
        ListNode* next;
        ListNode(int x): val(x) , next(nullptr){}
    };
    ListNode* head;

public:
    /** Initialize your data structure here. */
    MyLinkedList() : head(nullptr) {}
    
    /** 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 == nullptr )
            return -1;
        if( index <= 0 )
            return head->val;
        int count = 0 ; 
        ListNode* p = head;
        while( p && count < index )
        {
            p = p->next;
            count ++;
        }
        if(p)
            return p->val;
        else 
            return -1;
    }
    
    // 在链表头部插入节点
    void addAtHead(int val) {
        ListNode* node = new ListNode(val);
        if( head == nullptr)
        {
            head = node;
            return;
        }
        node->next = head;
        head = node;
    }
    
    // 在链表尾部插入节点
    void addAtTail(int val) {
        ListNode* node = new ListNode(val);
        // 链表为空 就返回
        if(head == nullptr)
        {
            head == node;
            return;
        }

        ListNode* p = head;
        while( p->next )
        {
            p = p->next;
        }
        p->next = node;
    }
    
    /** 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) {
        ListNode* node = new ListNode(val);
        if(index <= 0)
            addAtHead(val);
        int i = 0;
        ListNode* p = head;
        while(p && i<index - 1)
        {
            p=p->next;
            ++i;
        }
        if(p)
        {
            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) {
        if( head==nullptr)
            return ;
        if( index==0 )
        {
            ListNode* p = head;
            head = head->next;
            delete p;
            return;
        }
        ListNode* ps = finder(index-1);
        ListNode* p = finder(index);
        if( p && ps)
        {
            ps->next = p->next;
            return;
        }
        else{
            ps->next = nullptr;
            return ;
        }
        
    }

    // 给定下标,返回节点的指针 
    ListNode* finder (int index)
    {
        if( head == nullptr)
            return nullptr;
        if( index <= 0 )
            return head;
        int count = 0 ;
        ListNode* p = head;
        while ( p && count < index)
        {
            p = p->next;
            count ++;
        }
        if(p)
            return p;
        else 
            return nullptr;
    }
};

/**
 * 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);
 */
干啥啥不行,吃饭第一名
原文地址:https://www.cnblogs.com/jiangxinyu1/p/12285004.html