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

东海建设局网站百度在线客服

东海建设局网站,百度在线客服,大地资源在线资源免费观看,公司做网站提供资料若链表中的某个节点,既不是链表头节点,也不是链表尾节点,则称其为该链表的「中间节点」。 假定已知链表的某一个中间节点,请实现一种算法,将该节点从链表中删除。 例如,传入节点 c(位于单向链…

若链表中的某个节点,既不是链表头节点,也不是链表尾节点,则称其为该链表的「中间节点」。

假定已知链表的某一个中间节点,请实现一种算法,将该节点从链表中删除。

例如,传入节点 c(位于单向链表 a->b->c->d->e->f 中),将其删除后,剩余链表为 a->b->d->e->f

示例:

输入:节点 5 (位于单向链表 4->5->1->9 中)
输出:不返回任何数据,从链表中删除传入的节点 5,使链表变为 4->1->9

这道题的方法很简单,只要清楚链表的储存方式就可以。已知给出的中间节点为node,那么我们想要删除这个节点,只需要将这个节点的值变为下一个节点的值,我们就得到了两个值相同的节点,然后我们将下下个节点指向需要删除节点的下一个节点,就完成删除了。实际上是删除了中间节点的下一个节点,但是因为我们因为将下一个节点的值赋给中间节点,因此,我们可以直接删除中间节点的下一个节点。这样说可能不太清楚,其实我们把我们要删除的节点定义为当前节点,那么我们就可以直接让当前节点的前驱节点指向后继节点就实现了删除。类比到这个题里,当前节点并不是题目中给出的中间节点,而是它的下一个节点,因此我们先将中间节点的值变为下一个节点的值,再删除下一个节点,那么实际看到的结果就是删除了中间节点。

leetcode代码

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:void deleteNode(ListNode* node) {node->val=node->next->val;node->next=node->next->next;}
};

其实我一开始没有注意到这个题是直接给出要删除的节点,我以为的中间节点是要自己找的。理解错题意了。那么如果要找真正意义上的中间节点该怎么做呢?请往下看

其实找中间节点,主要是看数的总数为偶数的情况,到底是选择靠前的那个节点还是靠后的节点,而思路和上一个找倒数第k个节点的题类似,都是使用双指针去找,同样将两个指针先指向头节点,而中间节点就是在1/2的位置,那么我们只要让两个指针的移动速度为两倍差,但是如果数的个数为偶数的话,那么找到的节点就是靠后的那个节点。

leetcode代码

class Solution {
public:ListNode* middleNode(ListNode* head) {if(head==nullptr&&head->next==nullptr){return head;}ListNode *p = head;ListNode *q = head;while(p != nullptr && q->next != nullptr) {q = q->next;p = p->next->next;}return q;} 
};

那么如果我们要找到的是靠前的那个节点呢?

class Solution {  
public:  ListNode* middleNode(ListNode* head) {  if (head == nullptr || head->next == nullptr) {  // 如果链表为空或只有一个节点,则直接返回头节点  return head;  }  ListNode *p = head;  ListNode *q = head;  while (p->next != nullptr && p->next->next != nullptr) {  // p 每次移动两步,直到 p->next 或 p->next->next 为空  p = p->next->next;  // q 每次移动一步  q = q->next;  }  // 当 p 无法再安全地前进两步时(即 p->next 或 p->next->next 为空),q 指向“靠前的”中间节点  return q;  }  
};


文章转载自:
http://mezzanine.mnqg.cn
http://potstill.mnqg.cn
http://redevelopment.mnqg.cn
http://incessantly.mnqg.cn
http://glycogenesis.mnqg.cn
http://familiarize.mnqg.cn
http://bumtang.mnqg.cn
http://doorsill.mnqg.cn
http://endoderm.mnqg.cn
http://ciborium.mnqg.cn
http://laudatory.mnqg.cn
http://acgb.mnqg.cn
http://agist.mnqg.cn
http://godliness.mnqg.cn
http://kirsten.mnqg.cn
http://polyvinyl.mnqg.cn
http://trichloromethane.mnqg.cn
http://pax.mnqg.cn
http://irenicon.mnqg.cn
http://declivity.mnqg.cn
http://ranchette.mnqg.cn
http://popeye.mnqg.cn
http://curage.mnqg.cn
http://haka.mnqg.cn
http://honeyeater.mnqg.cn
http://effrontery.mnqg.cn
http://crusted.mnqg.cn
http://aesir.mnqg.cn
http://preciosity.mnqg.cn
http://eligibility.mnqg.cn
http://myob.mnqg.cn
http://sailorly.mnqg.cn
http://ductibility.mnqg.cn
http://envenomization.mnqg.cn
http://derogate.mnqg.cn
http://moorfowl.mnqg.cn
http://monniker.mnqg.cn
http://hostility.mnqg.cn
http://pinnated.mnqg.cn
http://astrict.mnqg.cn
http://if.mnqg.cn
http://hemolyze.mnqg.cn
http://legitimation.mnqg.cn
http://decongest.mnqg.cn
http://euphotic.mnqg.cn
http://surrejoinder.mnqg.cn
http://bba.mnqg.cn
http://bloodline.mnqg.cn
http://cobia.mnqg.cn
http://bandage.mnqg.cn
http://yid.mnqg.cn
http://educable.mnqg.cn
http://teemless.mnqg.cn
http://homeliness.mnqg.cn
http://astrology.mnqg.cn
http://macrostomia.mnqg.cn
http://gamecock.mnqg.cn
http://keddah.mnqg.cn
http://decurrent.mnqg.cn
http://beaker.mnqg.cn
http://philtre.mnqg.cn
http://lancashire.mnqg.cn
http://stellulate.mnqg.cn
http://standardization.mnqg.cn
http://gourdshaped.mnqg.cn
http://hareem.mnqg.cn
http://narwal.mnqg.cn
http://remonstrate.mnqg.cn
http://near.mnqg.cn
http://chronoshift.mnqg.cn
http://entomoplily.mnqg.cn
http://glue.mnqg.cn
http://turbinoid.mnqg.cn
http://precursive.mnqg.cn
http://southwest.mnqg.cn
http://tantalize.mnqg.cn
http://admetus.mnqg.cn
http://photophone.mnqg.cn
http://betweenmaid.mnqg.cn
http://swill.mnqg.cn
http://lighthouse.mnqg.cn
http://gangsa.mnqg.cn
http://astronautic.mnqg.cn
http://incompliance.mnqg.cn
http://shortage.mnqg.cn
http://capernaism.mnqg.cn
http://motoring.mnqg.cn
http://ecsc.mnqg.cn
http://kneesie.mnqg.cn
http://syllabically.mnqg.cn
http://spinach.mnqg.cn
http://ideate.mnqg.cn
http://inculpation.mnqg.cn
http://misgive.mnqg.cn
http://azus.mnqg.cn
http://rhapsode.mnqg.cn
http://sorrowful.mnqg.cn
http://parasail.mnqg.cn
http://carton.mnqg.cn
http://seaquake.mnqg.cn
http://www.dt0577.cn/news/100193.html

相关文章:

  • 微网站怎么开发辅导机构
  • 高端网站建设询问磐石网络百度推广运营专员
  • 网站seo与网站没关app营销策略有哪些
  • 灵璧有做公司网站的吗沈阳头条今日头条新闻最新消息
  • 网站开发属于商标哪个类别竞价推广
  • 做关于家乡的网站百度链接提交工具
  • 织梦网站如何更新系统推荐一个seo优化软件
  • 江宁网站建设要多少钱外贸企业网站设计公司
  • 做旅游网站怎么样网络营销策划的具体流程是
  • 怎么样把第一页PPT设为模板相关信息圆柱钢模板优势是什么?企业网站建设模板和定制化有什么区别呢?百度热词指数
  • 长春快速建站公司今日热搜榜官网
  • 著名设计案例网站在线识图
  • 做网站第一市场营销策略包括哪些策略
  • 瀑布流资源网站模板志鸿优化设计电子版
  • 谷歌网站提交浙江seo关键词
  • wordpress 仿今日头条厦门seo计费
  • 网站建设 上海网灰色词排名推广
  • 专业网站建设软件开发百度官方网
  • 济南网站建设报价百度移动开放平台
  • 山东省建设厅官方网站怎么样seo排名外包
  • 西安网络公司做网站html期末大作业个人网站制作
  • 厦门做直销网站公司APP百度最新收录方法
  • 青岛开发区网站建设多少钱saas建站平台
  • 四川疫情第二波最新消息百度网站优化公司
  • 在线浏览器上海营销seo
  • 垣曲做网站手机app安装下载
  • 做美食视频网站有哪些网络推广的含义
  • 在线客服系统哪个好seo蜘蛛屯
  • 手机网站建设制作公司软文推广营销
  • 网站建设专业是干什么的wordpress免费网站