零基础 网站,无忧网站模板,网站建设网站备案所需资料,虚拟主机网站怎么上传文件文章目录 前言一、链表分割二、环形链表I三、环形链表II四、链表的回文结构五、随机链表的复制 前言
一、链表分割
牛客网CM11#xff1a;链表分割- - -点击此处传送 题解#xff1a; 思路图#xff1a; 代码#xff1a;
二、环形链表I
力扣141#xff1a;环形链表… 文章目录 前言一、链表分割二、环形链表I三、环形链表II四、链表的回文结构五、随机链表的复制 前言
一、链表分割
牛客网CM11链表分割- - -点击此处传送 题解 思路图 代码
二、环形链表I
力扣141环形链表- - -点击此处传送 思路图: 扩展问题
代码
bool hasCycle(struct ListNode *head) {struct ListNode*fasthead,*slowhead;while(fast fast-next){//slow走一步slowslow-next;//fast走两步fastfast-next-next;//若相等相遇则有环返回true并退出程序if(fastslow){return true;}}//否则无环return false;
}三、环形链表II
力扣142环形链表II- - -点击此处传送 题解 思路图 代码
struct ListNode *detectCycle(struct ListNode *head) {struct ListNode*fasthead;struct ListNode*slowhead;while(fast fast-next){slowslow-next;fastfast-next-next;if(fastslow){struct ListNode*meetslow;while(head ! meet){headhead-next;meetmeet-next;}return meet;}}return NULL;
}四、链表的回文结构
牛客网OR36链表的回文结构- - -点击此处传送 思路图
代码
struct ListNode*reverseList(struct ListNode*head){struct ListNode*curhead;struct ListNode*newheadNULL;while(cur){struct ListNode*nextcur-next;cur-nextnewhead;newheadcur;curnext;}return newhead;}struct ListNode*middleNode(struct ListNode*head){struct ListNode*slowhead;struct ListNode*fasthead;while(fast fast-next){slowslow-next;fastfast-next-next;}return slow;}bool chkPalindrome(ListNode* head) {struct ListNode*midmiddleNode(head);struct ListNode*rheadreverseList(mid);while(head rhead){if(head-val ! rhead-val)return false;headhead-next;rheadrhead-next;}return true;}五、随机链表的复制
力扣138随机链表的复制- - -点击此处传送 思路图 代码
struct Node* copyRandomList(struct Node* head)
{struct Node*curhead;while(cur){struct Node*copy(struct Node*)malloc(sizeof(struct Node));copy-valcur-val;copy-nextcur-next;cur-nextcopy;curcopy-next;} curhead;while(cur){struct Node*copycur-next;if(cur-randomNULL){copy-randomNULL;}else{copy-randomcur-random-next;}curcopy-next;}curhead;struct Node*newheadNULL;struct Node*tailNULL;while(cur){struct Node*copycur-next;struct Node*nextcopy-next;if(tailNULL){newheadtailcopy;}else{tail-nextcopy;tailtail-next;}cur-nextnext;curnext;}return newhead;
}