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

万维网网站注册网站字体标准

万维网网站注册,网站字体标准,葛洲坝建设公司官网,wordpress 留言板展示文章目录 前言一、反转链表二、移除链表元素三、链表中倒数第K个结点四、相交链表五、链表的中间结点 前言 一、反转链表 力扣206:反转链表- - -点击此处传送 思路图: 方法一:改变指向 方法二: 代码: //方法一 /…

文章目录

  • 前言
  • 一、反转链表
  • 二、移除链表元素
  • 三、链表中倒数第K个结点
  • 四、相交链表
  • 五、链表的中间结点


前言

一、反转链表

力扣206:反转链表- - -点击此处传送
在这里插入图片描述
思路图:
方法一:改变指向
在这里插入图片描述
方法二:
在这里插入图片描述
代码:

//方法一
//改变指向
struct ListNode* reverseList(struct ListNode* head) {//判断空if(head==NULL){return NULL;}struct ListNode*n1,*n2,*n3;n1=NULL;n2=head;n3=head->next;while(n2){n2->next=n1;n1=n2;n2=n3;if(n3){n3=n3->next;}}return n1;
}//方法二
//头插
struct ListNode* reverseList(struct ListNode* head) {struct ListNode* cur = head;struct ListNode* newhead = NULL;while(cur){struct ListNode* next=cur->next;cur->next=newhead;newhead=cur;cur=next;}return newhead;
}

二、移除链表元素

力扣203:移除链表元素- - -点击此处传送
在这里插入图片描述
思路图:

在这里插入图片描述
方法2:
当然这题也可以使用带哨兵位的结点
在这里插入图片描述

代码

//方法1:
struct ListNode* removeElements(struct ListNode* head, int val) {struct ListNode*cur=head;struct ListNode*newhead=NULL;struct ListNode*tail=NULL;while(cur){if(cur->val!=val){if(tail==NULL){newhead=tail=cur;}else{tail->next=cur;tail=tail->next;}cur=cur->next;}else{struct ListNode*tmp=cur;cur=cur->next;free(tmp);}}if(tail)tail->next=NULL;return newhead; 
}//方法2:
struct ListNode* removeElements(struct ListNode* head, int val) {struct ListNode* cur=head;struct ListNode* tail=NULL;struct ListNode* newhead=NULL;//哨兵位newhead=tail=(struct ListNode*)malloc(sizeof(struct ListNode));while(cur){if(cur->val!=val){//尾插tail->next=cur;tail=tail->next;cur=cur->next;}else{struct ListNode*tmp=cur;cur=cur->next;free(tmp);}}tail->next=NULL;struct ListNode*tmp=newhead;newhead=newhead->next;free(tmp);return newhead;
}

三、链表中倒数第K个结点

牛客网:链表中倒数第K个结点- - -点击此处传送
在这里插入图片描述
思路图:
在这里插入图片描述
代码:

//牛客网代码模型
struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {// write code herestruct ListNode* fast=pListHead;struct ListNode* slow=pListHead;while(k--){//空链表if(fast==NULL)return NULL;fast=fast->next;}while(fast){fast=fast->next;slow=slow->next;}return slow;
}

四、相交链表

力扣160:相交链表- - -点击此处传送
在这里插入图片描述
思路图:
在这里插入图片描述
在这里插入图片描述

代码:

//思路2:
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {struct ListNode*curA=headA;struct ListNode*curB=headB;int lenA=0;int lenB=0;//计算链表A的长度while(curA->next){lenA++;curA=curA->next;}//计算链表B的长度while(curB->next){lenB++;curB=curB->next;}//无交点if(curA!=curB){return NULL;}//用绝对值求出差值int n=abs(lenA-lenB);struct ListNode*longList=headA;struct ListNode*shortList=headB;//若B长if(lenB>lenA){//长链表为BlongList=headB;//短链表为AshortList=headA;}//让长链表B先走差值while(n--){longList=longList->next;}//两链表一起走while(longList!=shortList){longList=longList->next;shortList=shortList->next;}return longList;
}

五、链表的中间结点

力扣876:链表的中间结点- - -点击此处传送
在这里插入图片描述
思路图:
在这里插入图片描述
代码:

struct ListNode* middleNode(struct ListNode* head) {struct ListNode*slow=head;struct ListNode*fast=head;while(fast && fast->next){slow=slow->next;fast=fast->next->next;}return slow;
}
http://www.yayakq.cn/news/184755/

相关文章:

  • 美橙互联 送网站三亚西岛
  • 网站底部背景做立体字的网站
  • 做企业网站设计方案天津市做公司网站的公司
  • 网站建设新发展专业科技公司网站欣赏
  • jsp做网站框架iis7 wordpress
  • 建设商城网站公司 百度百科广州各区进一步强化
  • dede手机网站更新app推广赚佣金
  • 大赛网站开发需求网站开发方法有哪些
  • 网站开发方案ppt做网站哪些
  • 网站后台浏览器前沿的设计网站
  • 做营销型网站 推广的好处wordpress添加icp
  • 思茅北京网站建设厦门网红打卡地
  • 织梦装修网站模板中国网站排名 优帮云
  • 广州 网站的设计公司律师网站素材
  • 免费模板素材网站有哪些网页制作免费版
  • 北京专业网站设计公司互联网官网
  • 郑州网站建设工资东莞大岭山俪仁妇产医院
  • 临沂网站排名优化wordpress导航栏怎么设置
  • 二级域名网站好不好做网站导航的
  • 涉县网站开发搭建网站服务器
  • 烟台网站建设方案咨询阿里云已备案域名购买
  • 一个新手如何做网站求职
  • 工业软件公司排名重庆搜索引擎seo
  • 郑州网站建设 论坛公司做自己的网站
  • 网站要怎么做的佛山白坭网站建设
  • 为什么要用php做网站注册商标流程
  • 网站制作q前端网站开发毕设类型
  • linux网站建设模板seo工具优化
  • 陕西省建设厅的网站优化大师下载安装
  • 做八年级题目的网站衣服定制app