一.链表深度拷贝
1.问题描述
力扣链接:. - 力扣(LeetCode)
2.问题详解
对于随即链表复制这道题的难点在于random的指向是随机的,我们无法通过random指向的节点的val来拷贝random的指向,因为可能有多个节点的val是相同的,这时候就无法确定random到底指向哪个节点,如下图所示:
因此,对于这个问题,我们采用将拷贝节点插入到原节点后面的方法,先将拷贝的节点插入原节点后面,只拷贝节点的val和next,暂时不拷贝random的指向,这时候拷贝节点与原节点创建了一个关联关系。由于拷贝节点恰好在原节点的后面一位,所以拷贝节点的random的指向恰好是原节点random指向的节点的next指针的指向,最后组成的新链表如下图所示:
将链表拷贝完毕后,只需要将拷贝的节点从原节点上面取下来尾插成新的节点,对于原节点来说,可以将原来的链表重新连接,也可以不做处理
3.代码:
typedef struct Node Node;
struct Node* copyRandomList(struct Node* head) {
Node*cur=head;
//拷贝节点插入原节点的后面
while(cur)
{
Node*copy=(Node*)malloc(sizeof(Node));
copy->val=cur->val;
copy->next=cur->next;
cur->next=copy;
cur=copy->next;
}
//控制random
cur=head;
while(cur)
{
Node*copy=cur->next;
if(cur->random==NULL)
{
copy->random=NULL;
}
else
{
copy->random=cur->random->next;
}
cur=copy->next;
}
//将拷贝的节点取下来尾插成新的链表
Node*newhead=NULL,*newtail=NULL;
cur=head;
while(cur)
{
Node*copy=cur->next;
Node*next=copy->next;
if(newhead==NULL)
{
newhead=newtail=copy;
}
else
{
newtail->next=copy;
newtail=copy;
}
cur->next=next;//将原来的链表重新链接
cur=next;
}
return newhead;
}
二.判断链表是否带环
1.问题描述
力扣链接:. - 力扣(LeetCode)
2.问题详解
对于这个问题我们采用快慢指针来解决。假设这是一个有环链表,快指针走的步数是慢指针的俩倍,因此当慢指针到达链表环的节点时,快指针已经在环里面走了一段路程,于是这就变成了快指针追击慢指针的问题,最后当快慢指针相等时说明它们相遇了,这是一个有环链表,返回true。如果是无环链表,当慢指针走到链表一半时,快指针已经到达尾节点或者是尾节点下一个节点。
fast刚入环时,slow走到链表的一半:
当slow进环,fast开始追击,当fast和slow相遇则说明是带环链表
3.代码
bool hasCycle(struct ListNode *head) {
struct ListNode*slow=head,*fast=head;
while(fast&&fast->next)
{
slow=slow->next;
fast=fast->next->next;
if(fast==slow)
{
return true;
}
}
return false;
}
4.面试相关经典问题
4.1问题一
为什么带环链表中的fast和slow一定会相遇,有没有可能会错过,永远追不上?请证明
如下图所示,我们假设当slow进环时,fast跟slow的距离为N
fast每走俩步,slow走一步,因此它们之间的距离就会不断缩小一步,构成一个递减的等差数列,不论N是什么值,最后的距离都会变为0,代表这时候fast追上了slow
4.2问题二
当slow一次走1步,fast一次走3步,4步,5步......n步时,fast和slow还一定会相遇吗?请证明
我们首先证明当slow走1步,fast走3步时的情况
总结一下就是当N为偶数在第一轮追击中就追上了,当N为奇数第一轮追击就会错过,距离变为C-1,然后又分为俩种情况:当C-1为偶数在第二轮追击中追上,C-1为奇数则重复上述行为,陷入无限追击中。如果同时存在N是奇数C是偶数,那么永远追不上,接下来证明一下这种情况是否存在
三.若链表带环返回入环第一个节点
1.问题描述
力扣链接:. - 力扣(LeetCode)
2.解法一
2.1问题详解
这个问题的难点在于确定链表有环后如何返回第一个交点,我们可以将fast和slow相遇的点取名为meet,接着meet和链表的头节点head一起走一步,meet和head相遇的点就是第一个交点,那为什么meet和head一定会相遇呢,接下来证明一下
2.2代码
struct ListNode *detectCycle(struct ListNode *head) {
struct ListNode*slow=head,*fast=head;
while(fast&&fast->next)
{
slow=slow->next;
fast=fast->next->next;
//相遇
if(fast==slow)
{
struct ListNode*meet=slow;
while(meet!=head)
{
meet=meet->next;
head=head->next;
}
return meet;
}
}
return NULL;
}
3.解法二
3.1问题详解
看下面的图,链表是有环链表,当fast和slow相遇时,将那个点取名为meet,将meet的下一个节点取名为newhead,再将meet的next指针置为空,这样就变成了俩个链表相交,求第一个交点的问题
3.2代码
struct ListNode* getInterdectionNode(struct ListNode*head1,struct ListNode*head2)
{
struct ListNode*curA=head1,*curB=head2;
int lenA=1,lenB=1;
while(curA->next)
{
curA=curA->next;
lenA++;
}
while(curB->next)
{
curB=curB->next;
lenB++;
}
if(curA!=curB)
{
return NULL;
}
int gap=abs(lenA-lenB);
struct ListNode*longside=head1,*shortside=head2;
if(lenB>lenA)
{
longside=head2;
shortside=head1;
}
while(gap--)
{
longside=longside->next;
}
while(longside!=shortside)
{
longside=longside->next;
shortside=shortside->next;
}
return longside;
}
struct ListNode *detectCycle(struct ListNode *head) {
struct ListNode*slow=head,*fast=head;
while(fast&&fast->next)
{
slow=slow->next;
fast=fast->next->next;
//相遇
if(fast==slow)
{ //struct ListNode*meet=slow;
struct ListNode*newhead=slow->next;
slow->next=NULL;
//转化为俩个链表相交问题
return getInterdectionNode(head,newhead);
}
}
return NULL;
}