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=c35Fp66CPV2 https://t3.kugou.com/song.html?id=c35Fp66CPV2
https://t3.kugou.com/song.html?id=c35Fp66CPV2

能力越大,越没责任

Bye ~~~



















