【算法】代码随想录之链表

文章目录

前言

一、移除链表元素(LeetCode--203)

二、设计链表(LeetCode--707)

三、反转链表(LeetCode--206)

四、两两交换链表中的节点(LeetCode--24)

五、删除链表的倒数第N个节点(LeetCode--19)

六、环形链表II(LeetCode--142)


前言

跟随代码随想录,学习链表相关的算法题目,记录学习过程中的tips。


一、移除链表元素(LeetCode--203)

【1】题目描述:

【2】解决思想:在当前节点判定下一个节点的val是否是目标值,若是则修改当前节点的next指针为下一个节点的next地址。为了使操作更加一致,创建一个头节点辅助操作。

【3】C++代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        struct ListNode* headNode = new ListNode(0, head);
        struct ListNode* pNode = headNode;
        while (pNode->next) {
            if (pNode->next->val == val) {
                auto tmp = pNode->next;
                pNode->next = pNode->next->next;
                delete tmp;
            } else {
                pNode = pNode->next;
            }
        }
        auto tmp = headNode->next;
        delete headNode;
        return tmp;
    }
};

【4】时间复杂度:O(N),只遍历了一遍链表。

【5】空间复杂度:O(1),只是开辟了一个头节点而已。


二、设计链表(LeetCode--707)

【1】题目描述:

【2】解决思想:题目比较简单,没有什么难点,大部分都是从前往后找到对应index的前一个节点,然后进行增加和删除。

【3】C++代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class MyLinkedList {
private:
    int size;//单链表长度
    ListNode *head;//头节点
public:
    MyLinkedList() {
        this->size = 0;
        this->head = new ListNode(0);
    }
    
    int get(int index) {
        if (index >= this->size || index < 0)
            return -1;
        ListNode *pNode = head->next;
        for (int i = 0; i < index; ++i) {
            pNode = pNode->next;
        }
        return pNode->val;
    }
    
    void addAtHead(int val) {
        addAtIndex(0, val);
    }
    
    void addAtTail(int val) {
        addAtIndex(this->size, val);
    }
    
    void addAtIndex(int index, int val) {
        if (index > this->size || index < 0)
            return;
        ListNode *pre = head;
        for (int i = 0; i < index; ++i) {
            pre = pre->next;
        }
        ListNode *tmp = new ListNode(val, pre->next);
        pre->next = tmp;
        this->size++;
    }
    
    void deleteAtIndex(int index) {
        if (index >= this->size || index < 0)
            return;
        ListNode *pre = head;
        for (int i = 0; i < index; ++i) {
            pre = pre->next;
        }
        ListNode *tmp = pre->next;
        pre->next = pre->next->next;
        delete tmp;
        this->size--;
    }
};

【4】时间复杂度:get函数为O(index),addAtHead函数为O(1),addAtTail函数为O(N),addAtIndex函数为O(index),deleteAtIndex函数为O(index)。

【5】空间复杂度:O(1)。


三、反转链表(LeetCode--206)

【1】题目描述:

【2】解决思想:遍历给定的单链表,将每个节点都重新链接到新链表中(采用头插法链接)。

【3】C++代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        //头插法实现链表反转
        ListNode *ret = NULL;
        ListNode *pNode = head;
        while (pNode) {
            //在原链表中取消链接
            head = pNode->next;
            //链接到新链表中
            pNode->next = ret;
            ret = pNode;
            //更新pNode为原链表下一个节点
            pNode = head;
        }
        return ret;
    }
};

【4】时间复杂度:O(N),只需要遍历一遍给定的链表。

【5】空间复杂度:O(1)。


四、两两交换链表中的节点(LeetCode--24)

【1】题目描述:

【2】解决思想:遍历给定的单链表,根据题目要求改变每个节点的next域即可。设置pre指针指向虚拟头节点,cur指针指向pre->next节点。修改pre和cur的next指针的指向即可,注意不要丢失后续节点的地址。操作完毕后,更新这两个指针为pre = cur且cur = pre->next。

【3】C++代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode *headNode = new ListNode(0, head);
        head = headNode;
        ListNode* pre = headNode, *cur = headNode->next;
        while (cur && cur->next) {
            pre->next = cur->next;
            ListNode* tmp = cur->next->next;
            cur->next->next = cur;
            cur->next = tmp;
            pre = cur;
            cur = pre->next;
        }
        head = headNode->next;
        delete headNode;
        return head;
    }
};

【4】时间复杂度:O(N),只需要遍历一遍给定的链表。

【5】空间复杂度:O(1)。


五、删除链表的倒数第N个节点(LeetCode--19)

【1】题目描述:

【2】解决思想:快慢指针法。先让快指针先走n+1步,然后快慢指针一起走,直到快指针指向NULL,那么慢指针最终指向的就是要删除元素的前一个元素。再执行相关的删除操作即可完成。

【3】C++代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* headNode = new ListNode(0, head);
        ListNode* slow = headNode, *fast = headNode;
        //快指针先走n+1步
        for (int i = 0; i < n + 1; ++i) {
            fast = fast->next;
        }
        //快慢指针同时走,使得慢指针指向删除元素的前一个元素
        while (fast) {
            slow = slow->next;
            fast = fast->next;
        }
        //删除指定元素
        ListNode* tmp = slow->next;
        slow->next = slow->next->next;
        delete tmp;
        //删除虚拟头节点
        head = headNode->next;
        delete headNode;
        return head;
    }
};

【4】时间复杂度:O(N),只需要遍历一遍给定的链表。

【5】空间复杂度:O(1)。

【6】启示:需要找到单链表倒数元素时,就可以使用快慢指针。


六、环形链表II(LeetCode--142)

【1】题目描述:

【2】解决思想:快慢指针法。快指针每次走两步,慢指针每次走一步。若它们相遇说明有环,若快指针到达末尾说明无环。有环时,让一个指针从头开始走,让一个指针从当前开始走,这两个指针每次都走一步,它们相遇就是环的入口。

【3】C++代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode *low = head, *fast = head;
        while (fast && fast->next) {
            fast = fast->next->next;
            low = low->next;
            if (low == fast) {
                //相遇已经说明有环
                //一个从头开始走,一个从当前开始走
                //相遇就是圈的入口
                low = head;
                while (low != fast) {
                    low = low->next;
                    fast = fast->next;
                }
                return low;
            }
        }
        return nullptr;
    }
};

【4】时间复杂度:O(N)

【5】空间复杂度:O(1)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值