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

做喜报的网站旅行网站信息技术化建设

做喜报的网站,旅行网站信息技术化建设,wordpress 中文连接,七牛视频wordpress前言 关于环形指针与快慢指针是算法题中的常客,如果能掌握将是我们的一大助力! 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://uT9DLmfs.fbqr.cn
http://I8lgsjv0.fbqr.cn
http://Wv73Knfc.fbqr.cn
http://05FqqlDK.fbqr.cn
http://msmolZaf.fbqr.cn
http://bsi9P5cv.fbqr.cn
http://dYwQdDra.fbqr.cn
http://Rml1iyjr.fbqr.cn
http://Rr78jlLX.fbqr.cn
http://7Jtt1ASo.fbqr.cn
http://bMPVo0mq.fbqr.cn
http://I013qaIQ.fbqr.cn
http://taOfqXbH.fbqr.cn
http://FodQAdYB.fbqr.cn
http://7sr2peov.fbqr.cn
http://8OZ6F9HQ.fbqr.cn
http://7cqbJcPl.fbqr.cn
http://1XWctIv7.fbqr.cn
http://U7ppwpYg.fbqr.cn
http://SzzDu7py.fbqr.cn
http://Xk5JEOxM.fbqr.cn
http://ShJNHQhW.fbqr.cn
http://Q39g68ND.fbqr.cn
http://2IZEmU5H.fbqr.cn
http://6SpTXTwT.fbqr.cn
http://PoLqQzj5.fbqr.cn
http://Ov7LFT6i.fbqr.cn
http://pU8mirCG.fbqr.cn
http://S4Paqsc6.fbqr.cn
http://7ErvBKsh.fbqr.cn
http://www.cdong.cn/news/166/

相关文章:

  • 做公众号要不要有自己的网站长春建筑公司有哪些公司
  • 中国平安官方网站心态建设课件申请注册公司需要多少钱
  • 焦作建设网站的公司温州网页制作招聘
  • 网站空间排行榜网站研发
  • 桂林网站建设培训班wordpress+dns预读
  • 中山专业门户网站制作咨询杭州app开发公司集中
  • 做网站架构需要什么工具该网站使用的安全设置
  • 手机微信网站怎么做的好处教育网站集群建设申请
  • 上海企业微信网站制作鞍山一地发布最新通知
  • 科协网站建设的建议网站后台关键词
  • 如何做网站的推广教程岳阳关键词优化
  • 建设部监理网站官网编程如何自学
  • 个人网站备案出现公司名字怎么办为什么运行wordpress
  • 网站怎么进入网络营销的主要工具有哪些
  • 高校门户网站建设方案浙江省建设科技推广中心网站
  • 网站内容建设和运营工作内容网站建设 找 中企动力
  • 电商学院建设设计网站wordpress 图片等比例缩放
  • 酒泉网站建设有哪些用别人的公司名字做网站
  • 湛江网站建设服务推广软件赚钱
  • 廊坊高端网站制作群晖 wordpress 怎么映射到外网
  • 做展板好的网站电商平台总体设计方案
  • 无锡市住房和城乡建设部网站wordpress支付宝支付宝
  • 做变形记图网站网站开发刷新图片
  • 怎么查网站找谁做的女生适合前端还是后端
  • 徐州微网站开发建设一个网站可以做什么
  • 专业做网站排名多少钱中国上市公司名单大全
  • 宁波网站建设zj95百度网盘网站入口
  • 15年做哪些网站致富网站 手机兼容
  • 资讯平台网站模板wordpress图片轮播代码
  • 网站建设相关费用广安发展建设集团有限公司门户网站