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

深圳网站制作运营做好评做销量的网站

深圳网站制作运营,做好评做销量的网站,贵州最新新闻,网络优化器下载hi,bro—— 目录 5、 链表分割 6、 链表的回文结构 7、 相交链表 8、 环形链表 【思考】 —————————————— DEAD POOL —————————————— 5、 链表分割 /* struct ListNode {int val;struct ListNode *next;ListNode(int x) : val(x), …

hi,bro——

目录

5、 链表分割

6、 链表的回文结构

7、 相交链表

8、 环形链表

【思考】

                 —————————————— DEAD POOL ——————————————


5、 链表分割

/*
struct ListNode {int val;struct ListNode *next;ListNode(int x) : val(x), next(NULL) {}
};*/
#include <functional>
class Partition {
public:ListNode* partition(ListNode* pHead, int x){//创建新的大小链表ListNode* lessHead,*lessTail;lessHead=lessTail=(ListNode*)malloc(sizeof(ListNode));ListNode* greaterHead,*greaterTail;greaterHead=greaterTail=(ListNode*)malloc(sizeof(ListNode));//创建新的头结点遍历数组ListNode* pcur=pHead;while(pcur){if(pcur->val<x){//尾插到小链表lessTail->next=pcur;lessTail=lessTail->next;}else{//尾插到大链表greaterTail->next=pcur;greaterTail=greaterTail->next;}pcur=pcur->next;}//大小链表首尾相连lessTail->next=greaterHead->next;greaterTail->next=NULL;ListNode* ret=lessHead->next;free(lessHead);free(greaterHead);lessHead=NULL;greaterHead=NULL;return ret;}
};

6、 链表的回文结构

在链表中不可回找,但是数组可以回找,所以我们可以把链表中的数据放到数组中。

思路1:创建新数组,遍历原链表,将链表中的值放到数组中,然后在数组中判断是否为回文结构。

因为题目中提前对链表的长度进行了限制,若不限制,空间复杂度为O(N)就不符合题目的条件了
/*
struct ListNode {int val;struct ListNode *next;ListNode(int x) : val(x), next(NULL) {}
};*/
class PalindromeList {
public:bool chkPalindrome(ListNode* A) {int arr[900]={0};ListNode* pcur=A;int i=0;//将链表中的数据赋给数组while(pcur){arr[i++]=pcur->val;pcur=pcur->next;}//i为链表中结点的个数//判断数组中的值是否为回文结构int left=0;int right=i-1;while(left<right){if(arr[left]!=arr[right]){return false;}left++;right--;}return true;}
};

思路2:反转链表,这种方法的时间复杂度为O(N),空间复杂度为O(1)

1)找原链表的中间结点,“快慢指针”;

2)将中间结点及之后的结点进行反转;

3)从原链表的头结点和新链表的头结点开始遍历比较。

/*
struct ListNode {int val;struct ListNode *next;ListNode(int x) : val(x), next(NULL) {}
};*/
class PalindromeList {
public://找中间结点ListNode* findMidNode(ListNode* phead){ListNode* slow,*fast;slow=fast=phead;while(fast&&fast->next){slow=slow->next;fast=fast->next->next;}return slow;}//反转链表ListNode* reverseList(ListNode* phead){ListNode* n1,*n2,*n3;n1=NULL;n2=phead;n3=n2->next;while(n2){n2->next=n1;n1=n2;n2=n3;if(n3)n3=n3->next;}return n1;}bool chkPalindrome(ListNode* A) {//1.找中间结点ListNode* mid=findMidNode(A);//2.反转链表ListNode* right=reverseList(mid);//3.遍历比较ListNode* left=A;while(right){if(left->val!=right->val)return false;left=left->next;right=right->next;}return true;}
};

7、 相交链表

思路:1)判断两个链表是否相等

           2)返回两个单链表相交的起始结点

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/typedef struct ListNode ListNode;
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {ListNode* l1=headA;ListNode* l2=headB;int countA=0;int countB=0;//求两个链表的长度while(l1){countA++;l1=l1->next;}while(l2){l2=l2->next;countB++;}//求链表长度差值的绝对值int gap=abs(countA-countB);//判断哪个是长短链表ListNode* longList=headA;ListNode* shortList=headB;if(countA<countB){longList=headB;shortList=headA; }//让两个链表在同一起跑线while(gap--){longList=longList->next;}//判断两个链表是否相交while(longList&&shortList){//相交if(longList==shortList){return longList;}longList=longList->next;shortList=shortList->next;}//不相交return NULL;
}

8、 环形链表

思路: 

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/typedef struct ListNode ListNode;
bool hasCycle(struct ListNode *head) {ListNode* slow=head;ListNode* fast=head;while(fast&&fast->next){slow=slow->next;fast=fast->next->next;if(slow==fast){return true;}}return false;
}

【思考】

头好痒,感觉要长脑子了


完—— 

Relaxing Time !

         —————————————— DEAD POOL ——————————————

https://t3.kugou.com/song.html?id=c35Fp66CPV2icon-default.png?t=N7T8https://t3.kugou.com/song.html?id=c35Fp66CPV2

能力越大越没责任 

Bye ~~~ 

http://www.yayakq.cn/news/332227/

相关文章:

  • 湖州住房和城乡建设局网站自己建商城型网站
  • php网站有点直播营销的优势有哪些
  • 1号网站建设辽宁省建设工程信息网拆除项目
  • 移动网站建站视频wordpress首页文字广告框架
  • 最新网站推广哪家好建湖做网站需要多少钱
  • 做原创的网站普同网站跟营销型网站的区别
  • 注册网站一年多少钱江津区做网站
  • 张家界seo资阳aso优化招聘
  • 网站建设答案外国人做免费视频网站
  • 请打开网站房屋装修设计图片欣赏
  • 手机网站架构邢台做网站信息
  • 建的网站打开很慢廉政网站 建设需求
  • 网络宣传网站建设价格如何用ip做网站
  • 绍兴建设网站嘉兴学网站建设
  • 怎么用php自己做网站下载室内设计排版模板网站有哪些
  • 学而思的网站哪里做的云南网络营销公司哪家好
  • 网站建设 蔬菜配送免费建立网站的有哪里
  • 手表大全网站网站制作的预算
  • 湖南营销型网站建设公司排名潮南最新消息今晚
  • 黄江镇网站建设WordPress的theme父主题命名
  • 新人如何自学做网站小制作简单手工
  • 网站开发属于什么大学专业网站怎么做百度才会收录
  • 兰州建设工程信息网站wordpress 背景音乐插件
  • 腾讯云wordpress搭建网站合肥建公司网站
  • 20个外国平面设计网站无锡网站优化排名推广
  • 北京网站平台建设安阳做网站多少钱
  • 和客户谈建网站怎么说网页生成pdf
  • 旅游网站系统设计公司网站怎么更新需要怎么做
  • 网站建设伍金手指下拉7用百度云服务器做网站
  • 辞职做网站优秀作文网站推荐