代码随想录训练营第三天和第四天|链表

本文介绍了链表的基本概念和各种操作,包括设计链表结构、移除元素、反转链表、两两交换节点、删除倒数第N个节点以及检测环形链表。通过实例展示了如何使用C++实现这些链表相关功能。
摘要由CSDN通过智能技术生成

在做题之前了解了链表有关的内容,在理解了链表的定义之后,做一些简单的列表题目还是比较轻松,尤其是做完了 707.设计链表 这个题目之后,对链表的理解加深了

203.移除链表元素

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) {
        ListNode* virtual_node = new ListNode(0);  // 设置一个虚拟头节点
        virtual_node->next = head;
        ListNode* cur = virtual_node;
        while (cur != NULL && cur->next != NULL) {
            if (cur->next->val == val) {
                ListNode* temp = cur->next;
                cur->next = cur->next->next;
                delete temp;
            } else {
                cur = cur->next;
            }
        }
        return virtual_node->next;
    }
};

707.设计链表

class MyLinkedList {
public:
    // 定义链表节点结构体
    struct LinkedNode {
        int val;
        LinkedNode *next;
        LinkedNode() : val(0), next(nullptr) {}
        LinkedNode(int x) : val(x), next(nullptr) {}
        LinkedNode(int x, LinkedNode *next) : val(x), next(next) {}
    };


    MyLinkedList() {
        _dummyHead = new LinkedNode(0);
        _size = 0;
    }
    
    int get(int index) {
        if (index < 0 || index > (_size-1)) {
            return -1;
        } else {
            LinkedNode* cur = _dummyHead;
            for (int i = 0; i <= index; i++) {
                cur = cur->next;
            }
            return cur->val;
        }
        
        

    }
    
    void addAtHead(int val) {
        LinkedNode* head = new LinkedNode(val);
        head->next = _dummyHead->next;
        _dummyHead->next = head;
        _size++;
    }
    
    void addAtTail(int val) {
        LinkedNode* cur = _dummyHead;
        // 计算当前链表最后一个元素的地址
        for (int i = 0; i < _size; i++) {
            cur = cur->next;
        }
        LinkedNode* newnode = new LinkedNode(val);
        cur->next = newnode;
        _size++;
    }
    
    void addAtIndex(int index, int val) {
        if (index < 0 || index > (_size)) {
            return;
        } else if (index == _size) {
            addAtTail(val);
        } else {
            LinkedNode* cur = _dummyHead;
            for (int i = 0; i < index; i++) {
                cur = cur->next;
            }
            LinkedNode* newnode = new LinkedNode(val);
            newnode->next = cur->next;
            cur->next = newnode;
            _size++;
        }
    }
    
    void deleteAtIndex(int index) {
        if (index < 0 || index > (_size - 1)) {
            return;
        } else {
            LinkedNode* cur = _dummyHead;
            for (int i = 0; i < index; i++) {
                cur = cur->next;
            }
            LinkedNode* temp = cur->next;
            cur->next = cur->next->next;
            delete temp;
            _size--;
        }
    }

private:
    LinkedNode* _dummyHead;
    int _size;
};

206.反转链表

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* temp; // 保存cur的下一个节点
        ListNode* cur = head;
        ListNode* pre = NULL;
        while(cur) {
            temp = cur->next;  // 保存一下 cur的下一个节点,因为接下来要改变cur->next
            cur->next = pre; // 翻转操作
            // 更新pre 和 cur指针
            pre = cur;
            cur = temp;
        }
        return pre;
    }
};

第二天的链表题目难度有所上升

24.两两交换链表中的节点

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* dummyHead = new ListNode(0);
        dummyHead->next = head;
        ListNode* cur = dummyHead;
        // ListNode* temp;
        // ListNode* temp2;
        while (cur->next != NULL && cur->next->next != NULL) {
            ListNode* temp = cur->next->next->next;
            ListNode* temp2 = cur->next;
            cur->next = cur->next->next;
            cur->next->next = temp2;
            cur->next->next->next = temp;
            

            cur = cur->next->next;
        }
        return dummyHead->next;

    }
};

19.删除链表的倒数第N个节点

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* dummyHead = new ListNode(0);
        dummyHead->next = head;
        ListNode* cur_fast = dummyHead;
        ListNode* cur_slow = dummyHead;
        ListNode* temp;
        for (int i = 0; i < n-1; i++) {
            cur_fast = cur_fast->next;
        }
        while (cur_fast->next != NULL) {
            cur_fast = cur_fast->next;
            temp = cur_slow;
            cur_slow = cur_slow->next;

        }
        // 删除操作
        temp->next = cur_slow->next;
        delete cur_slow;

        return dummyHead->next;

    }
};

下面两个题目都没什么头绪,都是看了视频和具体解答之后跟着写了一遍

160.相交链表

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode* curA = headA;
        ListNode* curB = headB;
        int lenA = 0, lenB = 0;
        while (curA != NULL) { // 求链表A的长度
            lenA++;
            curA = curA->next;
        }
        while (curB != NULL) { // 求链表B的长度
            lenB++;
            curB = curB->next;
        }
        curA = headA;
        curB = headB;
        // 让curA为最长链表的头,lenA为其长度
        if (lenB > lenA) {
            swap (lenA, lenB);
            swap (curA, curB);
        }
        // 求长度差
        int gap = lenA - lenB;
        // 让curA和curB在同一起点上(末尾位置对齐)
        while (gap--) {
            curA = curA->next;
        }
        // 遍历curA 和 curB,遇到相同则直接返回
        while (curA != NULL) {
            if (curA == curB) {
                return curA;
            }
            curA = curA->next;
            curB = curB->next;
        }
        return NULL;
    }
};

142.环形链表

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 *detectCycle(ListNode *head) {
        ListNode* fast = head;
        ListNode* slow = head;
        while(fast != NULL && fast->next != NULL) {
            slow = slow->next;
            fast = fast->next->next;
            // 快慢指针相遇,此时从head 和 相遇点,同时查找直至相遇
            if (slow == fast) {
                ListNode* index1 = fast;
                ListNode* index2 = head;
                while (index1 != index2) {
                    index1 = index1->next;
                    index2 = index2->next;
                }
                return index2; // 返回环的入口
            }
        }
        return NULL;
    }
};
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值