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

设计素材网站排名程序员做游戏还是做网站好

设计素材网站排名,程序员做游戏还是做网站好,零食网站建设的必要性,网站设计工作室公司文章目录 前言一、反转链表二、移除链表元素三、链表中倒数第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/444590/

相关文章:

  • 查备案网站备案临沂网站建设昂牛网络
  • 广西城乡和住房建设厅网站怎么样给网站的服务器续费
  • 西安做网站的公司哪家好常熟网站开发
  • asp网站建设报告书室内设计师参考网站
  • 中国建设银行网站无法访问网站关键词优化互点
  • 通用网址查询网站宁波做网站优化哪家好
  • 无忧网站建设哪家好松阳县建设局网站公示
  • 东西湖网站建设公司网站页面设计公司推荐
  • 玉环 网站建设成都建工路桥建设有限公司网站
  • 聊城高端网站制作wordpress首页显示友链
  • 关键词排名优化网站建设公司网站构架图怎么做
  • asp网站开发的背景与环境win7记事本做网站
  • 重庆聚百思网站开发南乐网站建设费用
  • 长沙网站建设的首选电商怎么做推广
  • 昆明网站建设创意校园网站如何管理
  • 网站被墙的原因淄川区建设局网站
  • 合肥快速做网站植物网站设计方案
  • 汕尾网站建设公司高端网站设计价格
  • 网站建设软件仿淘宝网站
  • 空间网站购买做网站需要花钱吗
  • 厦门正规网站建设公司广州网络平台
  • 响应式mvc企业网站源码百度浏览器官方下载
  • 邢台做网站多少钱关于文明网站建设存在的问题
  • 网站违法和做网站得有关系在线crm厂商
  • 湘潭网站建设企业全国分类信息网站排名
  • 长兴做网站网站栏目内容
  • 厦门网站的关键词自动排名淘宝小程序入口
  • 建设网站的内容贵阳做网站 优帮云
  • 创业网站怎么做的营销型网站有那些网站
  • 代账公司网站模板百度指数排名明星