栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

(算法)链表(基于牛客网)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

(算法)链表(基于牛客网)

模板 BM1 反转链表(基于头插法,涉及两个结点)

!操作只是涉及两个结点
!重要步骤:当前结点链接到头结点之前(一个指针)
!新链表:pre-> 原有链表:cur->nex->
头插法后新链表:cur->pre-> 原有链表:nex->
再次调整后:pre->node-> 原有链表:cur->nex->

ListNode* ReverseList(ListNode* pHead) {
		
        ListNode *cur=pHead;//需要插入头部的结点
        ListNode *pre=NULL;//始终指向新链表的头结点
        while(cur){
            ListNode *nex=cur->next;//保存当前操作结点的下一个位置
            cur->next=pre;//插入头部
            pre=cur;//头结点更新
            cur=nex;//当前操作结点移动到下一个位置
        }
        return pre;
    }
BM2 链表内指定区间反转(头插法,涉及三个结点)

!需要先建立头结点,用于返回最终结果
!pre指向插入结点的前一个结点,cur指向要插入的后一个位置,nex指向要变动的结点
原有链表:pre->cur->nex->
头插法后:pre->nex->cur->
下次循环第一步:pre->node->cur->nex->

    ListNode* reverseBetween(ListNode* head, int m, int n) {
        ListNode *res = new ListNode(-1);//手动添加头结点
        res->next = head;
        ListNode *pre = res;//要插入位置(在pre后面插入)
        
        for(int i = 1;i//找到要插入的位置
            pre = pre->next;
        }
        ListNode *cur = pre->next;//需要移动的结点的前一个位置(用于连接到下一个结点)
        for(int i = m;i
            ListNode *nex = cur->next;//需要移动的结点
            
            cur->next = nex->next;
            nex->next = pre->next;
            
            pre->next = nex;
        }
        return res->next;
    }
BM3 链表中的节点每k个一组翻转(递归,涉及两个点)

利用反转链表的方式

ListNode* reverseKGroup(ListNode* head, int k) {
        //找到每一组数组的头部
        ListNode *tail = head;
        for(int i=0;i
            if(tail == NULL){//如果不满足k个则直接返回当前头结点
                return head;
            }
            tail=tail->next;
        }
        //反转链表 同例题一
        ListNode *pre = NULL;
        ListNode *cur = head;
        for(int i=0;i
            ListNode *temp = cur->next;
            cur->next = pre;
            pre = cur;
            cur = temp;
        }
        head->next = reverseKGroup(tail,k);//递归调用
        return pre;
    }
BM4 链表中的节点每k个一组翻转 方法一
ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
    {
        ListNode *vhead = new ListNode(-1);
        ListNode *cur = vhead;
        while (pHead1 && pHead2) {
            if (pHead1->val <= pHead2->val) {
                cur->next = pHead1;
                pHead1 = pHead1->next;
            }
            else {
                cur->next = pHead2;
                pHead2 = pHead2->next;
            }
            cur = cur->next;
        }
        cur->next = pHead1 ? pHead1 : pHead2;
        return vhead->next;
    }
方法二 递归
    ListNode* Merge(ListNode* pHead1, ListNode* pHead2) {
        if(!pHead1)    return pHead2;
        if(!pHead2)    return pHead1;
        if(pHead1->val<=pHead2->val){
            pHead1->next = Merge(pHead1->next, pHead2);
            return pHead1;
        }else{
            pHead2->next = Merge(pHead1, pHead2->next);
            return pHead2;
        }
    }
BM5 链表中的节点每k个一组翻转 方法一 归并排序的思想(二路归并排序+两个链表的合并)

双指针:同方向访问两个链表、同方向访问一个链表(快慢指针)、相反方向扫描(对撞指针)
分治:将一个大而复杂的问题划分成多个性质相同但是规模更小的子问题,经常用递归来实现
!注意递归的用法

class Solution {
public:
	//合并两个链表
    ListNode *Merge2(ListNode *list1,ListNode *list2){
        if(list1 == NULL)    return list2;
        if(list2 == NULL)    return list1;
        ListNode* pre = new ListNode(-1);
        ListNode* cur = pre;
        while(list1 && list2){
            if(list1->val <= list2->val){
                cur->next = list1;
                list1=list1->next;
            }else{
                cur->next = list2;
                list2 = list2->next;
            }
            cur = cur->next;
        }
        cur->next = list1?list1:list2;
        return pre->next;
    }
    //二分法(递归实现)
    ListNode *divideMerge(vector &lists,int left,int right){
        if(left>right)    return NULL;
        else if(left == right)    return lists[left];
        int mid = (left+right)/2;
        return Merge2(divideMerge(lists, left, mid), divideMerge(lists, mid+1, right));
    }
    //方法函数
    ListNode *mergeKLists(vector &lists) {
        return divideMerge(lists, 0, lists.size()-1);
    }
};
方法二 优先队列(堆排序) BM6 判断链表中是否有环 方法一 双指针

使用两个指针,fast 与 slow。
它们起始都位于链表的头部。随后,slow 指针每次向后移动一个位置,而fast 指针向后移动两个位置。如果链表中存在环,则 fast 指针最终将再次与 slow 指针在环中相遇。
可以用快慢指针的原因见参考博文:快慢指针可以判断有环的原因
简而言之就是在快指针追赶慢指针时是一步一步追的,所以最终可以赶上。
So easy!

public:
    bool hasCycle(ListNode *head) {
        ListNode *slow = head,*fast = head;
        while(skow&&fast){
            slow = slow -> next;
            fast = fast -> next->next;
            if(fast == slow)
                return true;
        }
        return false;
    }
方法二 哈希表

我们遍历链表中的每个节点,并将它记录下来;一旦遇到了此前遍历过的节点,就可以判定链表中存在环。借助哈希表可以很方便地实现
1、遍历链表,并将访问过的结点存储到哈希表中
2、判断结点是否在哈希表中,若存在则返回 true
3、遍历结束,则返回 false

class Solution {
public:
    bool hasCycle(ListNode *head) {
        unordered_set ha;
        while(head!=NULL&&head->next!=NULL){
          if(ha.count(head)) return head;//如果存在
          ha.insert(head);
          head=head->next;
       }
        return NULL;

    }
};
BM7 链表中环的入口结点 方法一 哈希表

走一步用一个set(集合)保存
注:此处用unordered_set(底层用哈希表实现,不排序),而set用红黑树实现(排序)
且map和unordered_map使用相同的方式

在这里插入代码片
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/977369.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号