day03&day04链表专题

链表基本

707 设计链表

存在的问题:

  1. 链表和节点的初始化;
  2. 在index位置插入元素需要检查index的合法性。
class MyLinkedList {
public:
    struct LinkNode{
        int val;
        struct LinkNode* next;
        LinkNode(int val):val(val),next(nullptr){}
    };

    MyLinkedList() {
        //初始化MyLinkedList对象
        _size = 0;
        _dummyHead=new LinkNode(0);
    }
    
    int get(int index) {
        LinkNode* cur = _dummyHead->next;
        if(index >= _size) return -1;
        while(index--){
            cur = cur->next;
        }
        return cur->val;
    }
    
    void addAtHead(int val) {
        //头插
        LinkNode* point = new LinkNode(val);
        point->next=_dummyHead->next;
        _dummyHead->next=point;
        _size++;
    }
    
    void addAtTail(int val) {
        //尾插
        LinkNode* point = new LinkNode(val);
        LinkNode* cur = _dummyHead;
        while(cur->next){
            cur=cur->next;
        }
        cur->next = point;
        _size++;
    }
    
    void addAtIndex(int index, int val) {
        //找链表中index-1的位置。index=size插入到尾部
        if(index > _size) return;
        LinkNode* cur = _dummyHead;
        LinkNode* point = new LinkNode(val);
        while(index--){
            cur=cur->next;
        }
        point->next = cur->next;
        cur->next=point;
        _size++;
    }
    
    void deleteAtIndex(int index) {
        //找链表index-1位置
        if(index >= _size) return;
        LinkNode* cur = _dummyHead;
        while(index--){
            cur=cur->next;
        }
        //删除cur->next;
        LinkNode* temp = cur->next;
        cur->next=cur->next->next;
        delete temp;
        _size--;

    }
private:
    int _size;//链表长度
    LinkNode* _dummyHead;//虚拟头节点
};

/**
 * 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);
 */

203 移除链表元素

注意:

  • C++删除链表元素需要delete空间;new的元素也需要手动delete。
  • while中的if-else,不要忘记else。
  • 建立虚拟节点dummyHead使删除操作一致。
/**
 * 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) {
        ListNode* dummyHead = new ListNode(0);
        dummyHead->next = head;
        ListNode* cur=dummyHead;//指向满足要求的前一个位置
        while(cur->next){
            if(cur->next->val==val){
                ListNode* temp = cur->next;
                cur->next=cur->next->next;
                delete temp;
            } 
            else{
                cur = cur->next;
            }
        }
        ListNode* temp = dummyHead->next;
        delete dummyHead;
        return temp;

    }
};

双指针

206 反转链表

  • pre和cur的初值;
  • 由于pre和cur移动需要保存cur->next,引入temp,注意temp赋值的位置。
  • 结束cur指向null,pre指向反转链头。
/**
 * 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* pre=nullptr;
        ListNode* cur = head;
        ListNode* temp;
        while(cur){
            temp = cur->next;//保存下一个节点
            cur->next=pre;//反转
            pre = cur;
            cur=temp;
        }
        return pre;
    }
};

24 反转相邻

/**
 * 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) {
        if(head==NULL || head->next == NULL) return head;
        //没有元素/只有一个元素
        ListNode* pre = head;
        ListNode* cur = head->next;
        ListNode* temp = NULL;
        head = cur;
        while(true){
            temp = cur->next;
            cur->next = pre;
           if(temp == NULL||temp->next==NULL){
                pre->next = temp;
                break;
            }
            else{
                pre->next=temp->next;
            }
            pre = temp;
            cur=temp->next;
        } 
        return head;
    }
};

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

/**
 * 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) {
        //fast和slow
        //找倒数第n+1的位置
        ListNode* dummyHead = new ListNode(0);
        dummyHead->next=head;
        ListNode* fast = dummyHead->next;
        ListNode* slow = dummyHead;
        while((n--) && (fast!=NULL)){
            fast=fast->next;//找到正数下标为n的元素
        }
        if(n != -1) return head;
        //n大于链表总长
        while(fast){
            fast = fast->next;
            slow = slow->next;
        }
        ListNode* tmp = slow->next;
        slow->next = slow->next->next;
        delete tmp;
        return dummyHead->next;

    }
};

两个链表相交

双重循环
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        //链表的结构,只有一个后继节点
        ListNode* tmpA = headA, *tmpB = headB;
        while(tmpA){
            tmpB = headB;
            //注意初始化
            while(tmpB){
                if(tmpA == tmpB) return tmpA;
                tmpB=tmpB->next;
            }
            tmpA=tmpA->next;
        }
        return nullptr;
    }
};
双指针

在这里插入图片描述

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        //链表的结构,只有一个后继节点
        ListNode* tmpA = headA, *tmpB = headB;
        while(tmpA!=tmpB){
            if(tmpA == nullptr) tmpA=headB;
            else tmpA=tmpA->next;
            if(tmpB == nullptr) tmpB=headA;
            else tmpB=tmpB->next;
        }
        if(tmpA == tmpB) return tmpA;
        return nullptr;
    }
};

142环形链表

哈希表
//错误代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
 #define MAX 200001
 #define MID 100000

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        //哈希表
        int *record=(int *)malloc(sizeof(int)*MAX);
        for(int i=0; i < MAX; i++) record[i]=0;

        ListNode* p = head;
        while(p){
            int count = (p->val)+MID;
            record[count]++;
            if(record[count]>1) return p;
            p=p->next;
        }
        free(record);
        return nullptr;
    }
};

//错误原因:不是数字重复代表有环,是位置重复

//正确代码

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        unordered_set<ListNode *> visited;
        while (head != nullptr) {
            if (visited.count(head)) {
                return head;
            }
            visited.insert(head);
            head = head->next;
        }
        return nullptr;
    }
};
双指针

两次相遇:

  • 是否有环:fast走两步,slow走一步,fast和slow相遇,则说明有环;
  • 环的入口地址确定:f=s+nb; f=2s; 得到s=nb;环的入口为a+nb,所以slow再走a步到环入口,从head开始再走a步到环入口。slow和fast=head同时出发,相遇处即为环入口。
    在这里插入图片描述

参考解法

/**
 * 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 * fast = head, * slow = head;
        int flag = 1;//标记链表有没有环,1无环
        
        while(fast&&fast->next&&flag){
        //注意执行fast->next->next;条件fast!=NULL&&fast->next!=NULL
            fast=fast->next->next;
            slow=slow->next;
            if(slow==fast) flag=0;
            //fast追上slow,标记有环
        }
        if(!flag){
        //有环,找环的入口位置
            fast = head;
            while(fast!=slow){
                fast=fast->next;
                slow=slow->next;
            }
            return fast;
        }
        return nullptr;
    }
};

总结:

  • 链表逻辑结构:只有一个后继节点,所以如果两个链表的后继节点相同,两个链表从这个点之后的元素是重合的,见题“两个链表相交”。
  • 链表插入:在这里插入图片描述
  • 链表删除:找被删节点的上一个节点
  • 链表查找:
    ①查找下标为index(从0开始)
    cur = _dummyHead->next;
    while(index–) { cur=cur->next; }
    ②查找下标为index的前一个节点
    cur = _dummyHead;
    while(index–) { cur=cur->next; }
  • 21
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值