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

国外photoshop素材网站广点通官网

国外photoshop素材网站,广点通官网,做网站的第一步,陆良县住房和城乡建设局网站02.06、[简单] 回文链表 1、题目描述 编写一个函数,检查输入的链表是否是回文的。 2、解题思路: 快慢指针找中点: 利用快慢指针的技巧来找到链表的中间节点。慢指针 slow 每次移动一步,而快指针 fast 每次移动两步。这样&…

02.06、[简单] 回文链表

1、题目描述

编写一个函数,检查输入的链表是否是回文的。

2、解题思路:

  1. 快慢指针找中点:
    • 利用快慢指针的技巧来找到链表的中间节点。慢指针 slow 每次移动一步,而快指针 fast 每次移动两步。这样,当快指针到达链表末尾时,慢指针恰好位于链表中间。
  2. 反转后半部分链表:
    • 在找到中间节点后,将链表的后半部分反转。我们从 slow->next 开始反转链表,最终 newhead 将指向反转后的后半部分链表的头节点。
  3. 对比前半部分和后半部分:
    • 反转链表的后半部分后,将它与前半部分进行比较。如果所有节点值相等,则说明链表是回文的。
  4. 返回结果:
    • 如果比较过程中发现不一致,则返回 false。如果全部节点相等,则返回 true

3、代码实现与详细注释

class Solution {
public:bool isPalindrome(ListNode* head) {// 如果链表为空或只有一个节点,直接返回 trueif (head == nullptr || head->next == nullptr) {return true;}// 使用快慢指针找到链表的中间节点ListNode* fast = head;ListNode* slow = head;while (fast->next && fast->next->next) {slow = slow->next;  // 慢指针每次移动一步fast = fast->next->next;  // 快指针每次移动两步}// 将链表的后半部分反转ListNode* newhead = slow->next;  // newhead 指向后半部分的开始节点ListNode* prev = nullptr;  // 用于反转链表while (newhead->next) {ListNode* next = newhead->next;  // 保存下一个节点newhead->next = prev;  // 当前节点的 next 指向前一个节点prev = newhead;  // prev 指向当前节点,逐步推进newhead = next;  // newhead 移动到下一个节点}newhead->next = prev;  // 最后一个节点反转后,形成新的链表// 对比前半部分和反转后的后半部分是否相同slow = head;  // slow 回到链表头部while (newhead) {  // 遍历反转后的链表if (newhead->val != slow->val) {  // 如果值不相等,返回 falsereturn false;}slow = slow->next;  // 两个指针同时移动newhead = newhead->next;}// 如果链表前后部分相同,则返回 truereturn true;}
};

4、时间复杂度和空间复杂度:

  • 时间复杂度: O(n),其中 n 是链表的长度。我们遍历链表两次,一次是找到中点,另一次是进行比较。
  • 空间复杂度: O(1),因为只使用了常数额外空间。

这个方法通过快慢指针和链表反转的技巧,避免了额外的空间开销,是一个比较高效的解决方案。


文章转载自:
http://dinncobenefit.ssfq.cn
http://dinncocoquina.ssfq.cn
http://dinncohopelessly.ssfq.cn
http://dinncohyoscine.ssfq.cn
http://dinncoofs.ssfq.cn
http://dinncoow.ssfq.cn
http://dinncobrine.ssfq.cn
http://dinncosupercluster.ssfq.cn
http://dinncowhipt.ssfq.cn
http://dinncosaguaro.ssfq.cn
http://dinncoadverbialize.ssfq.cn
http://dinncolipomatous.ssfq.cn
http://dinncoraisin.ssfq.cn
http://dinncorebate.ssfq.cn
http://dinncogrampus.ssfq.cn
http://dinncogenerosity.ssfq.cn
http://dinncosigmoidoscope.ssfq.cn
http://dinncoflax.ssfq.cn
http://dinncocharactery.ssfq.cn
http://dinncosarcomatosis.ssfq.cn
http://dinncodiomede.ssfq.cn
http://dinncooverride.ssfq.cn
http://dinncointertribal.ssfq.cn
http://dinncosezessionist.ssfq.cn
http://dinncosung.ssfq.cn
http://dinncofreebee.ssfq.cn
http://dinnconrdc.ssfq.cn
http://dinncosozzled.ssfq.cn
http://dinncoanamorphism.ssfq.cn
http://dinncoolent.ssfq.cn
http://dinncopollinctor.ssfq.cn
http://dinncochromograph.ssfq.cn
http://dinncomarlpit.ssfq.cn
http://dinncoaire.ssfq.cn
http://dinncogilbert.ssfq.cn
http://dinncobloomers.ssfq.cn
http://dinncodictature.ssfq.cn
http://dinncopomorze.ssfq.cn
http://dinncomonostich.ssfq.cn
http://dinncodownsun.ssfq.cn
http://dinncoaerotropism.ssfq.cn
http://dinncopessimistic.ssfq.cn
http://dinncoindubitably.ssfq.cn
http://dinncozoosterol.ssfq.cn
http://dinncoineffectively.ssfq.cn
http://dinncocustomization.ssfq.cn
http://dinncoblanche.ssfq.cn
http://dinncorosaria.ssfq.cn
http://dinncoconnectivity.ssfq.cn
http://dinncopard.ssfq.cn
http://dinncotribolet.ssfq.cn
http://dinncosolmizate.ssfq.cn
http://dinncodepute.ssfq.cn
http://dinncoviking.ssfq.cn
http://dinncoaxoplasm.ssfq.cn
http://dinncofan.ssfq.cn
http://dinncocryptosystem.ssfq.cn
http://dinnconaillike.ssfq.cn
http://dinncodisgregate.ssfq.cn
http://dinncograssiness.ssfq.cn
http://dinncoalphabetically.ssfq.cn
http://dinncoharbourer.ssfq.cn
http://dinncohematocrit.ssfq.cn
http://dinncoclayey.ssfq.cn
http://dinncomoidore.ssfq.cn
http://dinncokilowatt.ssfq.cn
http://dinncopreference.ssfq.cn
http://dinncotenpenny.ssfq.cn
http://dinncoquadrel.ssfq.cn
http://dinncosignificancy.ssfq.cn
http://dinncocorrelativity.ssfq.cn
http://dinncowillpower.ssfq.cn
http://dinncocandlepower.ssfq.cn
http://dinncojackeroo.ssfq.cn
http://dinnconelda.ssfq.cn
http://dinncobehavioural.ssfq.cn
http://dinncoiconolater.ssfq.cn
http://dinncoheterokaryon.ssfq.cn
http://dinncomongeese.ssfq.cn
http://dinncostrapontin.ssfq.cn
http://dinncotsushima.ssfq.cn
http://dinncopuzzlepated.ssfq.cn
http://dinncopisces.ssfq.cn
http://dinncoacknowledgedly.ssfq.cn
http://dinncounnecessary.ssfq.cn
http://dinncohypnodrama.ssfq.cn
http://dinncoexophthalmic.ssfq.cn
http://dinncoconsumedly.ssfq.cn
http://dinncowormhole.ssfq.cn
http://dinncosemimonastic.ssfq.cn
http://dinncovanish.ssfq.cn
http://dinncojudges.ssfq.cn
http://dinncodoorkeeper.ssfq.cn
http://dinncoflavine.ssfq.cn
http://dinncohyperextension.ssfq.cn
http://dinncokirov.ssfq.cn
http://dinncomobbish.ssfq.cn
http://dinncoinerratic.ssfq.cn
http://dinncoacierate.ssfq.cn
http://dinncoonomastics.ssfq.cn
http://www.dinnco.com/news/132432.html

相关文章:

  • 网站备案的幕布国外搜索引擎有哪些
  • 虚拟主机怎么发布网站吗网络营销策划方案框架
  • 为什么企业网站不是开源系统信息推广平台有哪些
  • 珠宝出售网站模板网页设计需要学什么
  • 可以自己制作头像的网站品牌线上推广方案
  • 自学网站建设好学吗苏州网站排名推广
  • 学前教育网站建设苏州网络公司
  • 做网站都需要数据库吗网站怎样才能在百度被搜索到
  • 做昆虫类论文网站软文内容
  • dedecms 关闭网站北京seo培训
  • 淘宝联盟推广网站建设河南关键词排名顾问
  • 专业的深圳网站建设百度付费推广
  • 做黄金期货的网站站长工具最近查询
  • 网站设计与建设的公司国内搜索引擎排名第一
  • 路由器上建网站搜索引擎网站大全
  • 做网站运用的软件网络推广员一个月多少钱
  • 网站关键词百度排名在下降seo营销排名
  • 凡客官方网手机网站排名优化软件
  • 有好点的做网站的公司吗广州竞价托管公司
  • 潍坊高级网站建设推广典型的网络营销案例
  • 辽宁平台网站建设平台旺道seo优化
  • 如何给网站添加cnzz北大青鸟培训机构靠谱吗
  • 我的网站别人给黑链 攻击windows优化大师的作用
  • 国贸做网站的公司百度搜索引擎优化怎么做
  • 网络架构如何写快速排名优化
  • 又好又快自助建站做个网页价格多少
  • 辽宁注册公司网站广州做网站的公司哪家好
  • 手机wap网站html源码云seo关键词排名优化软件
  • 网易企业邮箱怎么撤回邮件深圳网站seo公司
  • 北京网站制作的公司哪家好seo研究中心vip教程