LeetCode--链表1-单链表

Posted jiangxinyu1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
 */

以上是关于LeetCode--链表1-单链表的主要内容,如果未能解决你的问题,请参考以下文章

[算法] leetcode单链表相关题目详解

[算法] leetcode单链表相关题目详解

看一遍就理解,图解单链表反转

看一遍就理解,图解单链表反转

LeetCode每日一刷 --- 手撕单链表习题

Leetcode练习(Python):链表类:第206题:反转链表:反转一个单链表。