链表的经典习题(画图详解)


前言


1.链表的分割

在这里插入图片描述

在这里插入图片描述

class Partition {
public:
    ListNode* partition(ListNode* pHead, int x) {
        
        ListNode* smalltail, *smallhead;
        ListNode* bigtail, *bighead;
        smallhead = smalltail = (ListNode*)malloc(sizeof(ListNode));
        bighead = bigtail = (ListNode*)malloc(sizeof(ListNode));
        ListNode* cur = pHead;
       while(cur)
    {
        if(cur->val < x)
        {
            smalltail->next = cur;
            smalltail = smalltail->next;
        }
        else
        {
            bigtail->next = cur;
            bigtail = bigtail->next;
        }
        cur = cur->next;
    }
    smalltail->next=bighead->next;
    bigtail->next=NULL;
    pHead=smallhead->next;
    free(smallhead);
    free(bighead);
    return pHead;
    }
};

2.链表的回文结构

在这里插入图片描述

class PalindromeList {
public:
    bool chkPalindrome(ListNode* A) 
    {
        if(A==NULL)
            return false;
        else if(A->next==NULL)
            return true;
    struct ListNode*fast=A;
    struct ListNode*slow=A;

     while (fast&&fast->next) 
     {
        slow = slow->next;
        fast = fast->next->next;
     }
    
    struct ListNode*cur=slow;
    struct ListNode*rhead=NULL;
    while(cur)
    {
        struct ListNode*next=cur->next;
        cur->next=rhead;
        rhead=cur;
        cur=next;
    }
   while(A&&slow)
   {
       if((A->val)!=(rhead->val))
       {
            return false;
       }
       A=A->next;
       slow=slow->next;
   }
   return true;
 }
};

-    恶趣味请1

3.相交链表

在这里插入图片描述

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) 
{
    struct ListNode *curA=headA,*curB=headB;
    int lenA=0;
    int lenB=0;
    while(curA->next)
    {
        ++lenA;
        curA=curA->next;
    }
    while(curB->next)
    {
        ++lenB;
        curB=curB->next;
    }
    if(curA!=curB)
    {
        return NULL;
    }
    int pos=abs(lenA-lenB);
    struct ListNode *longlist=headA,*shortlist=headB;
    if(lenA<lenB)
    {
        longlist=headB;
        shortlist=headA;
    }
    while(pos--)
    {
        longlist=longlist->next;
    }
    while(longlist!=shortlist)
    {
        longlist=longlist->next;
        shortlist=shortlist->next;
    }
    return shortlist;

}

在这里插入图片描述

4.环形链表

在这里插入图片描述

我们可以定义两个指针,一个速度为2,一个速度为1,这样就成了追及问题,如果能追上就代表有环

bool hasCycle(struct ListNode *head) 
{
    struct ListNode *slow=head;
    struct ListNode *fast=head;
    while(fast&&fast->next)
    {
        slow=slow->next;
        fast=fast->next->next;
        if(slow==fast)
        return true;
    }
    return false;
}

思路很简单轻松实现
在这里插入图片描述

5. 环形链表2

在这里插入图片描述
这道题让我们来找到进入循环的点,我们先来记住一个二级结论,一个指针从链表头开始走,另一个和指针从快慢指针相遇的地方走,他们相遇的点就是进入循环的点
我们先来代码实现一下

struct ListNode *detectCycle(struct ListNode *head) 
{
    struct ListNode *slow=head;
    struct ListNode *fast=head;
    while(fast&&fast->next)
    {
        slow=slow->next;
        fast=fast->next->next;
        if(slow==fast)
        {
             struct ListNode *meet=slow;
             while(meet!=head)
             {
                 head=head->next;
                 meet=meet->next;
             }
             return meet;
        }
        
    }
    return NULL;
}

接下来我们来证明这个结论

在这里插入图片描述

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值