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

【数据结构初阶】单链表经典算法题十二道——得道飞升(中篇)

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.lryc.cn/news/408455.html

相关文章:

  • CTF ssrf 基础入门 (一)
  • IP地址在后端怎么存才好?
  • 《通讯世界》是什么级别的期刊?是正规期刊吗?能评职称吗?
  • go get的原理
  • jenkins替换配置文件
  • C# Web控件与数据感应之 填充 HtmlTable
  • HAL库源码移植与使用之SPI驱动VS1053音频解码
  • RK3568 Linux 平台开发系列讲解(内核入门篇):从内核的角度看外设芯片的驱动
  • 初识C++ · AVL树(2)
  • LLM:归一化 总结
  • 蓝桥杯 2024 年第十五届省赛真题 —— 最大异或结点
  • AV1技术学习:Loop Restoration Filter
  • 如何使用python实现自动化办公?干货满满!
  • QT Creator下载安装详细教程(保姆级教程)
  • 无人机公司销售需要什么资质
  • 代码自动化重构工具OpenRewrite介绍
  • Win11安装Docker
  • Windows电脑如何启动RTSP服务实现本地摄像头数据共享
  • 探索 Spring WebFlux:构建响应式 Web 应用
  • C# 植物大战僵尸
  • css 作业 2
  • axios在vue中的使用
  • FastAPI(七十七)实战开发《在线课程学习系统》接口开发-- 课程编辑和查看评论
  • 【JavaEE初阶】线程的概念及创建
  • 0727,学什么学,周六就应该休息!!!!!
  • 【C#】获取DICOM图像像素的像素值
  • k8s多集群管理工具kubecm
  • 通过 WSL 2 在Windows 上挂载 Linux 磁盘
  • 【C#】在一个给定的宽、高范围内,获取到该多边形内部的所有坐标集合?
  • json的数据结构