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

石家庄网站制作武汉打开一个网站

石家庄网站制作武汉,打开一个网站,武汉游戏seo,上海建材网站建设今天讲一些关于链表的Oj题,相信你看完对链表又提升一个档次。 题目一 思路一 遍历一遍链表是Val值得时候free这个,然后我们往后走,一直走到末尾空指针得时候,新链表就是我们得答案,那我们用代码来表示一下吧。 struct…

在这里插入图片描述
今天讲一些关于链表的Oj题,相信你看完对链表又提升一个档次。

题目一

在这里插入图片描述
思路一
遍历一遍链表是Val值得时候free这个,然后我们往后走,一直走到末尾空指针得时候,新链表就是我们得答案,那我们用代码来表示一下吧。


struct ListNode* removeElements(struct ListNode* head, int val){struct ListNode*cur=head;struct ListNode*pre=NULL;while(cur){if(cur->val==val){if(pre==NULL){head=cur->next;free(cur);cur=head;}else{pre->next=cur->next;free(cur);cur=pre->next;}}else{pre=cur;cur=cur->next;}}return head;
}

思路二
不是val我们就拿下来,是val就跳过,放到新链表中。

struct ListNode* removeElements(struct ListNode* head, int val){struct ListNode*tail=NULL;struct ListNode*cur=head;head=NULL;while(cur){if(cur->val==val){cur=cur->next;}else{if(tail==NULL){head=tail=cur;}else{tail->next=cur;tail=tail->next;}cur=cur->next;}}if(tail)tail->next=NULL;   return head;
}

思路三
带哨兵位得头节点得方法,是val拿下来,不是跳过。


struct ListNode* removeElements(struct ListNode* head, int val){struct ListNode*cur=head;head=(struct ListNode*)malloc(sizeof(struct ListNode));struct ListNode*tail=head;tail->next=NULL;while(cur){if(cur->val==val){struct ListNode*del=cur;cur=cur->next;free(del);}else{tail->next=cur;cur=cur->next;tail=tail->next;}}tail->next=NULL;struct ListNode*del=head->next;free(head);return del;}

题目二

在这里插入图片描述

这题我们可以改变指向,给三个指针变量,变方向就可以解决。

struct ListNode* reverseList(struct ListNode* head){if(head==NULL)return NULL;struct ListNode*cur=head;struct ListNode*pre=NULL;struct ListNode*next=cur->next;while(cur){cur->next=pre;pre=cur;cur=next;if(next)next=next->next;}return pre;
}

思路二
头插到新链表就可以了。

struct ListNode* reverseList(struct ListNode* head){struct ListNode*cur=head;struct ListNode*phead=NULL;while(cur){struct ListNode*next=cur->next;cur->next=phead;phead=cur;cur=next;}return phead;
}

题目三

在这里插入图片描述

用快慢指针,快走两步,慢走一步,就可以解决。

struct ListNode* middleNode(struct ListNode* head){struct ListNode*slow=head;struct ListNode*fast=head;while(fast && fast->next){slow=slow->next;fast=fast->next;if(fast)fast=fast->next;}return slow;
}

题目四

struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {struct ListNode*slow=pListHead;struct ListNode*fast=pListHead;while((k--)){if(fast==NULL)return NULL;fast=fast->next;}while(fast){fast=fast->next;slow=slow->next;}return slow;
}

在这里插入图片描述

这题和快慢指针差不多,先让快指针走k步,然后同时走,结束条件就是快指为空的时候。

今天就先分享四道题目,后面再继续分享几道!!!谢谢大家观看

http://www.15wanjia.com/news/9498.html

相关文章:

  • 供应链网站制作注册网站需要多少钱?
  • 上海做网站搜索一下马来西亚的网络营销专业好就业吗
  • ASP做旅游网站代码三只松鼠网络营销策略
  • 网站建设免备案免费空间南京seo网站管理
  • 温州网站建设温州网站制作一键优化大师下载
  • 什么是品牌vi设计江门seo外包公司
  • centos7系统做网站网站备案
  • 个人做二次元网站怎么赚钱如何做推广
  • 学校诗歌网站建设推广费用一般多少钱
  • 外贸网站论文seo技术网网
  • 苏州网站建设品牌韶山百度seo
  • 网站建设中跳转页面源码竞价是什么意思
  • 深圳网站建设公司pestl分析软件培训机构有哪些?哪个比较好
  • 房地产网站素材重庆官网seo分析
  • 中山做展示型网站seo网站推广的主要目的
  • 建站时候源码有验证怎么办郑州seo推广优化
  • 网站设计公司市场容量网站建设培训机构
  • phpcmsv9手机网站源码最新的销售平台
  • 站长之家ping检测免费网页制作平台
  • 织梦网站怎么居中企业培训平台
  • 网站效果图怎么做的营销网站推荐
  • 免费做网站软件下载企业网站推广方法
  • 网站的风格设计佛山抖音seo
  • 国外一家做乳胶衣视频的网站百度平台营销软件
  • 网站建设企业排名柳市网站制作
  • 做网站需要字体授权广告网址
  • c做项目的网站广告策划
  • 网站三要素关键词 描述怎么做app推广引流方法
  • 做游戏模板下载网站有哪些人力资源和社会保障部
  • 男女情感类网站十大骗子教育培训机构