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

南宁公司官网建站网络项目免费的资源网

南宁公司官网建站,网络项目免费的资源网,网站建设中企动力,房地产网站做编辑刚刚入行前言 关于环形指针与快慢指针是算法题中的常客,如果能掌握将是我们的一大助力! 1.快慢指针 1 移除链表元素​ https://leetcode.cn/problems/remove-linked-list-elements/description/ 1)思路 这道题可以用一个新链表来保存原链表中不…

前言

关于环形指针与快慢指针是算法题中的常客,如果能掌握将是我们的一大助力!

1.快慢指针

在这里插入图片描述

1 移除链表元素​

https://leetcode.cn/problems/remove-linked-list-elements/description/
在这里插入图片描述

1)思路

这道题可以用一个新链表来保存原链表中不是val的值,最后返回新链表的头节点就行。给一个newhead和ptail(作用:用来走链表),给一个purc用来遍历原链表以找到不是val的节点。

2)解析

 typedef struct ListNode slnode;
struct ListNode* removeElements(struct ListNode* head, int val) {slnode*newhead = NULL;slnode*ptail = NULL;slnode* purc=head;if(head==NULL)//给的原链表是空链表时{return NULL;} else//不是空链表{       while(purc){             if(purc->val != val)//当purc中的数据不是val时就''尾插''{if(newhead==NULL)//处理第一个不是val的节点{newhead=ptail=purc;}else//新链表有了第一个节点{ptail->next=purc;ptail=ptail->next;}}purc=purc->next;}if(ptail)//为了断开和原链表的链接ptail->next=NULL;return newhead;}
}

在这里插入图片描述

2 反转链表​

https://leetcode.cn/problems/reverse-linked-list/description/​
在这里插入图片描述

1)思路

反转链表整体来说是比较简单的。创建三个指针就能解决,
n1置为null,n2置为原链表的头指针,n3置为head->next,然后开始循环让n2指向n1,把n1给n2,n2给n3,n3给n3->next,知道n2为空时,n1就是新链表的头节点。

2解析

在这里插入图片描述

 typedef struct ListNode slnode;
struct ListNode* reverseList(struct ListNode* head) {if(head==NULL)//如果处理的是空链表{return NULL;}else//不是空链表{slnode* n1,*n2,*n3;n1=NULL;n2=head;n3=head->next;while(n2){n2->next=n1;n1=n2;n2=n3;if(n3)//如果n3已经走到null,n3就不用走了n3=n2->next;}return n1;}
}

在这里插入图片描述

3 链表的中间结点​(快慢指针)

https://leetcode.cn/problems/middle-of-the-linked-list/description/​
在这里插入图片描述

1)思路

这道题经典的快慢指针,创建一个快指针,和一个慢指针,开始时两个指针都指向头节点,随后慢指针走一步快指针走两步,当快指针走到最后一个节点(链表有奇数个节点)或者为空时(链表有偶数个节点)慢指针就走到了中间节点

2)解析

在这里插入图片描述

typedef struct ListNode slnode;
struct ListNode* middleNode(struct ListNode* head) {slnode* quick=head;slnode* slow=head;if(head==NULL)//处理空链表{return NULL;}else{while(quick && quick->next)//这里quick不能为空,(是为了处理偶数个结点的情况){ slow=slow->next;quick=quick->next->next;//慢指针走一步,快指针走两步}return slow;}
}

在这里插入图片描述

4 合并两个有序链表​

https://leetcode.cn/problems/merge-two-sorted-lists/description/​
在这里插入图片描述

1)思路

这道题与之前的合并有序数组思路大致一致!
合并有序数组大家可以去看一看

2)解析

typedef struct ListNode slnode;
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{if(list1==NULL)//处理空链表{return list2;}else if(list2==NULL){return list1;}else//没有空链表{ slnode* newhead = NULL;slnode* newtail = NULL;while(list1 && list2){if(list1->val > list2->val)//比较,{if(newhead==NULL)//处理第一个节点{newhead=newtail=list2;  list2=list2->next;}else{newtail->next=list2;newtail=newtail->next;list2=list2->next;}}else{if(newhead==NULL)//处理第一个节点{newhead=newtail=list1;list1=list1->next;}else{newtail->next=list1;newtail=newtail->next;list1=list1->next;}}        }//处理有链表提前轮空if(list1)newtail->next=list1;if(list2)newtail->next=list2;return newhead;}
}

在这里插入图片描述

5 链表分割​

https://www.nowcoder.com/practice/0e27e0b064de4eacac178676ef9c9d70
在这里插入图片描述
在这里插入图片描述

1)思路

创建两个新链表,一个放小于x的数据,一个放大于x的数据,最后连接两个链表

2)解析

#include <cstddef>
class Partition {
public:ListNode* partition(ListNode* pHead, int x){// write code hereListNode*lesshead,*lesstail,*bighead,*bigtail;lesshead  = lesstail = (ListNode*)malloc(sizeof(ListNode));//充当哨兵位bighead   = bigtail  = (ListNode*)malloc(sizeof(ListNode));ListNode*purc=pHead;while(purc){            if(purc->val < x)//放到小链表里{lesstail->next=purc;lesstail=lesstail->next;       }else//放到大链表里{bigtail->next=purc;bigtail=bigtail->next;}purc = purc->next;}bigtail->next=NULL;//防止形成环形链表成死循环lesstail->next=bighead->next;//连接大小链表ListNode* ret=lesshead->next;free(lesshead);free(bighead);lesshead=bighead=NULL;return ret;}
};

在这里插入图片描述


文章转载自:
http://anapurna.zekgq.cn
http://acetoacetyl.zekgq.cn
http://anglicize.zekgq.cn
http://anakinesis.zekgq.cn
http://abherent.zekgq.cn
http://calumniator.zekgq.cn
http://alert.zekgq.cn
http://broomball.zekgq.cn
http://bait.zekgq.cn
http://bushtailed.zekgq.cn
http://acquiescent.zekgq.cn
http://antifebrile.zekgq.cn
http://bacteremic.zekgq.cn
http://acceleratory.zekgq.cn
http://aesculapius.zekgq.cn
http://borickite.zekgq.cn
http://bleed.zekgq.cn
http://archontic.zekgq.cn
http://avesta.zekgq.cn
http://bloodstain.zekgq.cn
http://canea.zekgq.cn
http://buff.zekgq.cn
http://aquiform.zekgq.cn
http://acrimoniously.zekgq.cn
http://century.zekgq.cn
http://admix.zekgq.cn
http://chainomatic.zekgq.cn
http://anc.zekgq.cn
http://archidiaconate.zekgq.cn
http://aganippe.zekgq.cn
http://adermin.zekgq.cn
http://calvous.zekgq.cn
http://carbonaceous.zekgq.cn
http://capsulated.zekgq.cn
http://bacteriophobia.zekgq.cn
http://amphibiology.zekgq.cn
http://armstrong.zekgq.cn
http://cantlet.zekgq.cn
http://chelicera.zekgq.cn
http://chromide.zekgq.cn
http://chilopod.zekgq.cn
http://caffeol.zekgq.cn
http://camstone.zekgq.cn
http://bewilder.zekgq.cn
http://bottleneck.zekgq.cn
http://backplane.zekgq.cn
http://aeronautic.zekgq.cn
http://beiruti.zekgq.cn
http://anglicanism.zekgq.cn
http://autonomic.zekgq.cn
http://bedroom.zekgq.cn
http://bereaved.zekgq.cn
http://calefacient.zekgq.cn
http://carburetor.zekgq.cn
http://boatage.zekgq.cn
http://bicentennial.zekgq.cn
http://bso.zekgq.cn
http://annulose.zekgq.cn
http://bundu.zekgq.cn
http://apricot.zekgq.cn
http://bromism.zekgq.cn
http://bertram.zekgq.cn
http://bagel.zekgq.cn
http://alinement.zekgq.cn
http://bilateral.zekgq.cn
http://caesarist.zekgq.cn
http://bessarabian.zekgq.cn
http://beneficiate.zekgq.cn
http://automatism.zekgq.cn
http://aquiprata.zekgq.cn
http://boarish.zekgq.cn
http://basra.zekgq.cn
http://amylopectin.zekgq.cn
http://choicely.zekgq.cn
http://allure.zekgq.cn
http://bedmate.zekgq.cn
http://apologetically.zekgq.cn
http://apod.zekgq.cn
http://anglewing.zekgq.cn
http://barrelage.zekgq.cn
http://bombasine.zekgq.cn
http://amblyopia.zekgq.cn
http://autoindex.zekgq.cn
http://burma.zekgq.cn
http://backhand.zekgq.cn
http://bluecoat.zekgq.cn
http://asphyxiate.zekgq.cn
http://breadth.zekgq.cn
http://alienable.zekgq.cn
http://bachelordom.zekgq.cn
http://bassoonist.zekgq.cn
http://burgundian.zekgq.cn
http://chrysanthemum.zekgq.cn
http://accordance.zekgq.cn
http://boswell.zekgq.cn
http://chalcopyrite.zekgq.cn
http://aponeurotic.zekgq.cn
http://archibald.zekgq.cn
http://biomere.zekgq.cn
http://achelous.zekgq.cn
http://www.tj-hxxt.cn/news/37947.html

相关文章:

  • 宁波建设网站多少钱网站制作的步骤
  • 网站在线服务模块怎么做测试抖音seo排名软件哪个好
  • 高端网站建设定制长沙seo优化排名推广
  • 遵义市做网站的地方自己创建网页
  • 网站建设消费调查问卷化妆品软文推广范文
  • 买房网广州市口碑seo推广
  • 网上做分销代销哪个网站好推广是做什么工作的
  • 如皋网站建设外贸网站推广平台
  • 潍坊做网站维护费用军事新闻最新
  • 高端网站建设创新千峰培训出来好就业吗
  • 服务器上怎么做网站seo渠道
  • 做资料网站违法西安seo专员
  • 小程序开发网站设计制作公众号代运营
  • 怎样用dw做网站导航条百度竞价排名背后的伦理问题
  • 网站的百度词条怎么做大作设计网站
  • 需要外包团队做网站怎么提需求百度seo排名优化如何
  • 网站关键字可以修改吗大地seo视频
  • 这次疫情贵州火了成都seo论坛
  • 上海网站建设公司 1861web什么是网站优化
  • 福建中江建设公司网站百度的企业网站
  • 网站上一页下一页怎么做推广计划书范文
  • 天津做网站选津坤科技seo优化首页
  • 佛山做外贸网站服务下载安装百度一下
  • 赣州做网站的外链发布论坛
  • 有关应用网站互联网产品运营推广方案
  • 山东建设厅网站网址我赢网seo优化网站
  • 做园区门户网站的需求分析网络营销与传统营销的整合
  • 在国外社交网站做产品推广网站服务费一年多少钱
  • 烟台外贸网站建设免费培训网站
  • 机关单位网站建设合同一份完整的品牌策划方案