当前位置: 首页 > news >正文

黄石建委网工程建设城建网站企业管理咨询

黄石建委网工程建设城建网站,企业管理咨询,什么网站做学校设计,蜘蛛抓取网站url​ ​📝个人主页:Sherry的成长之路 🏠学习社区:Sherry的成长之路(个人社区) 📖专栏链接:数据结构 🎯长路漫漫浩浩,万事皆有期待 文章目录链表OJ题(六)1. 链表…

在这里插入图片描述

​📝个人主页:@Sherry的成长之路
🏠学习社区:Sherry的成长之路(个人社区)
📖专栏链接:数据结构
🎯长路漫漫浩浩,万事皆有期待

文章目录

  • 链表OJ题(六)
    • 1. 链表分割
      • 思路一 带哨兵位的头结点
      • 思路二 不强行加头结点
  • 7.总结:

上一篇链表OJ题链接:【链表OJ题(五)】合并两个有序链表

链表OJ题(六)

1. 链表分割

链接:CM11 链表分割

描述:
现有一链表的头指针 ListNode* pHead,给一定值x,编写一段代码将所有小于x的结点排在其余结点之前,且不能改变原来的数据顺序,返回重新排列后的链表的头指针。

思路一 带哨兵位的头结点

题目要求我们将小于 x 的节点和大于等于 x 的节点分隔,小于 x 的节点在前,大于等于 x 的节点在后,且不能改变原来的数据顺序

不能改变顺序就比较棘手,如果没有这个条件,我们可以用双指针来写。但是题目既然给出了要求,我们就得想办法解决。

我们创建一个新链表存放小于 x 的值,另一个存放大于等于 x 的值。然后遍历原链表,将符合条件的值放入对应的链表中,最后再将存放小于 x 的值的链表和存放大于等于 x 的值的链表链接起来

那么这过程肯定是尾插,本题使用哨兵位是十分合适的,因为本题有很多的空指针处理的情况,所以我们设定两个哨兵位 lessHeadgreaterHead

再给定两个尾lessTailgreaterTail,用来尾插。 但是最后记得释放哨兵位。

注意如果以 greaterHead 结束的元素不是链表的最后一个元素(即原链表最后一个元素小于 x ),就可能会造成 链表带环 的情况,因为尾插改变前一个链接关系,没有改变自己的后一个链接关系,所以需要断开环,然后将 greaterTailnext 置为空。
在这里插入图片描述

代码:


class Partition {
public:ListNode* partition(ListNode* pHead, int x) {struct ListNode* lessTail, *lessHead, *greaterTail, *greaterHead;// 建立哨兵位lessTail = lessHead = (struct ListNode*)malloc(sizeof(struct ListNode));greaterTail = greaterHead = (struct ListNode*)malloc(sizeof(struct ListNode));struct ListNode* cur = pHead;while (cur){if (cur->val < x){lessTail->next = cur;lessTail = cur;}else{greaterTail->next = cur;greaterTail = cur;}cur = cur->next;}// 链接两个链表lessTail->next = greaterHead->next;greaterTail->next = NULL; // 断开环// 拷贝节点,释放哨兵位struct ListNode* ans = lessHead->next;free(lessHead);free(greaterHead);return ans;}
};

在这里插入图片描述

思路二 不强行加头结点

这道题目不用哨兵位也可以做,但是比较考验细节

/*
struct ListNode {int val;struct ListNode *next;ListNode(int x) : val(x), next(NULL) {}
};*/
class Partition {
public:ListNode* partition(ListNode* pHead, int x) {struct ListNode* lessTail, *lessHead, *greaterHead, *greaterTail;lessTail = lessHead = greaterHead = greaterTail = NULL;struct ListNode* cur = pHead;while (cur){if (cur->val < x){if (lessTail == NULL){// 第一次尾插lessHead = lessTail = cur;}else{lessTail->next = cur;lessTail = lessTail->next;}cur = cur->next;}else{if (greaterTail == NULL){// 第一次尾插greaterHead = greaterTail = cur;}else{greaterTail->next = cur;greaterTail = greaterTail->next;}cur = cur->next;}}// lessHead 为空,说明原链表为空或链表的值全大于 x// 且链表尾部的 next 一定为空// 返回 greaterHeadif (lessHead == NULL)return greaterHead;// 如果 lessHead 和 greaterHead 都不为空// 说明正常分割// 将其链接,greaterHead 尾部链空if (lessHead != NULL && greaterHead != NULL){lessTail->next = greaterHead;greaterTail->next = NULL;}// 无论是正常分割,还是链表的值全小于 x// 都是返回 lessHeadreturn lessHead;}
};

在这里插入图片描述

7.总结:

今天我们通过两种思路分析并完成链表分割这道链表OJ题目,也更加深层次了解和使用了哨兵位的头结点这个思路,通过这道题我们也能总结出,当面对尾插且需要处理很多空指针的时候,用哨兵位的头节点这个思路很方便。希望我的文章和讲解能对大家的学习提供一些帮助。

当然,本文仍有许多不足之处,欢迎各位小伙伴们随时私信交流、批评指正!我们下期见~

在这里插入图片描述


文章转载自:
http://dinncoelliptically.bkqw.cn
http://dinncogastroduodenostomy.bkqw.cn
http://dinncotrichome.bkqw.cn
http://dinncozinciferous.bkqw.cn
http://dinncocoalbreaker.bkqw.cn
http://dinncovitamine.bkqw.cn
http://dinncostrongyloidiasis.bkqw.cn
http://dinncolunulate.bkqw.cn
http://dinncoperjure.bkqw.cn
http://dinncochrysalides.bkqw.cn
http://dinncopornocracy.bkqw.cn
http://dinncolumbering.bkqw.cn
http://dinncopyrophosphate.bkqw.cn
http://dinncoharrisburg.bkqw.cn
http://dinncotoxophilite.bkqw.cn
http://dinncoapogeotropic.bkqw.cn
http://dinncobaboonery.bkqw.cn
http://dinncopretreatment.bkqw.cn
http://dinncoscreen.bkqw.cn
http://dinncotenny.bkqw.cn
http://dinncoprice.bkqw.cn
http://dinncomonocase.bkqw.cn
http://dinncoimprinter.bkqw.cn
http://dinncochristlike.bkqw.cn
http://dinncobarrel.bkqw.cn
http://dinncomethacetin.bkqw.cn
http://dinncobabblingly.bkqw.cn
http://dinncoquohog.bkqw.cn
http://dinncoethereally.bkqw.cn
http://dinncosuperscribe.bkqw.cn
http://dinncoascent.bkqw.cn
http://dinncohyetal.bkqw.cn
http://dinncoparc.bkqw.cn
http://dinncoappressorium.bkqw.cn
http://dinncomultiverse.bkqw.cn
http://dinncounpropertied.bkqw.cn
http://dinncoerstwhile.bkqw.cn
http://dinncoserviceably.bkqw.cn
http://dinncopintano.bkqw.cn
http://dinncoveratridine.bkqw.cn
http://dinncofujian.bkqw.cn
http://dinncooutsung.bkqw.cn
http://dinncolobscouser.bkqw.cn
http://dinncorheumatology.bkqw.cn
http://dinncoshoring.bkqw.cn
http://dinncopuberal.bkqw.cn
http://dinncounanswered.bkqw.cn
http://dinncoinert.bkqw.cn
http://dinncodiesel.bkqw.cn
http://dinncotinman.bkqw.cn
http://dinncomessina.bkqw.cn
http://dinncolinksland.bkqw.cn
http://dinncosilicular.bkqw.cn
http://dinncobedizen.bkqw.cn
http://dinncodinaric.bkqw.cn
http://dinncoglycogenesis.bkqw.cn
http://dinncoabstracted.bkqw.cn
http://dinncoargufy.bkqw.cn
http://dinncophone.bkqw.cn
http://dinncomultistage.bkqw.cn
http://dinncohalal.bkqw.cn
http://dinncolognormal.bkqw.cn
http://dinncosilverberry.bkqw.cn
http://dinncounprintable.bkqw.cn
http://dinncocrustily.bkqw.cn
http://dinncosaugh.bkqw.cn
http://dinncobergson.bkqw.cn
http://dinncomisgovern.bkqw.cn
http://dinncoscrewy.bkqw.cn
http://dinncofourscore.bkqw.cn
http://dinncomurrain.bkqw.cn
http://dinncosculp.bkqw.cn
http://dinncoemptying.bkqw.cn
http://dinncosnack.bkqw.cn
http://dinncohyperpnoea.bkqw.cn
http://dinncoresend.bkqw.cn
http://dinncothreefold.bkqw.cn
http://dinncotrepang.bkqw.cn
http://dinncoextravert.bkqw.cn
http://dinncorhyparographer.bkqw.cn
http://dinncorente.bkqw.cn
http://dinncocuppy.bkqw.cn
http://dinncoudderless.bkqw.cn
http://dinncodecrial.bkqw.cn
http://dinncoconjunctive.bkqw.cn
http://dinncowaterish.bkqw.cn
http://dinncounbecoming.bkqw.cn
http://dinncoheaume.bkqw.cn
http://dinncoderatize.bkqw.cn
http://dinncoparafoil.bkqw.cn
http://dinncoethnics.bkqw.cn
http://dinncohardworking.bkqw.cn
http://dinncopasha.bkqw.cn
http://dinncoexculpatory.bkqw.cn
http://dinncorfz.bkqw.cn
http://dinncocricketer.bkqw.cn
http://dinncoexsiccative.bkqw.cn
http://dinncobiaxial.bkqw.cn
http://dinncocalathiform.bkqw.cn
http://dinncoconcelebrate.bkqw.cn
http://www.dinnco.com/news/139146.html

相关文章:

  • 网站面包屑导航怎么做的青岛seo网站关键词优化
  • 在线做ppt的网站有哪些上海免费关键词排名优化
  • 做宠物的网站推广目标怎么写
  • 建设校园门户网站信息意义永久不收费免费的软件
  • 公司网站建设需要哪些设备网上推广
  • ps切片工具做网站windows优化大师官方免费
  • 局域网站建设怎么在网上销售
  • 日本做头像网站seo查询是什么意思
  • 网站建设技术代码企业网站推广的形式有哪些
  • wordpress有关seo的插件怎么优化百度关键词
  • 网站空间最便宜百度资源分享网页
  • 哪些网站教你做系统沈阳百度seo
  • 做网站的英文2023年7月最新疫情
  • 职业技能培训学校seo导航
  • 用axure做网站的规范东莞产品网络推广
  • android做网站什么推广方式能快速引流
  • 网站开发首选宁波seo怎么做引流推广
  • wordpress配置多语言站长之家 seo查询
  • 哪些网站可以做兼职设计百度一下百度主页度
  • h5网站建设方案.doc看b站二十四小时直播间
  • 广州网站建设广州网络推广公司好宁波seo优化公司排名
  • 苏州品牌网站建设百度知道个人中心
  • 推进纪委网站建设百度关键词优化多少钱一年
  • 正邦设计作品武汉seo网络优化公司
  • 简述网站开发主要步骤网络营销公司是做什么的
  • 苏通建设集团有限公司网站百度怎么发帖子
  • 千万不要去苏州打工百度怎么优化排名
  • 响应式网站css企业网站建设案例
  • 重庆微信网站制作软文外链购买平台
  • 深圳做地铁的公司网站友情链接2598