力扣刷题7

64 阅读2分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第4天,点击查看活动详情


链表分割

在这里插入图片描述

题目的意思是这样的,小于x的节点在前面,大于等于x的节点排在后面,其相对位置不变。看下面的更能更好的理解。

在这里插入图片描述

解题思路

我们把原链表分成2个链表,第一个链表为小于x的链表,第二个链表为大于等于x的链表。最后第二个链表连接到第一个链表的后面。返回第一个链表的地址。 形成两个链表的过程中,遍历原链表。两个链表都是带头节点的。

代码

class Partition {
public:
    ListNode* partition(ListNode* pHead, int x) {
        struct ListNode *list1=(struct ListNode *)malloc(sizeof(struct ListNode));
        struct ListNode *list2=(struct ListNode *)malloc(sizeof(struct ListNode));
        struct ListNode *head1,*head2,*tail1,*tail2;
        list1->next=NULL;
        list2->next=NULL;
        head1=tail1=list1;
        head2=tail2=list2;
        while(pHead)
        {
            if(pHead->val<x)
            {
                tail1->next=pHead;
                pHead=pHead->next;
                tail1=tail1->next;
            }
            else
            {
                tail2->next=pHead;
                pHead=pHead->next;
                tail2=tail2->next;
            }
        }
        tail2->next=NULL;
        tail1->next=head2->next;
        
        struct ListNode *head=head1->next;
        free(list1);
        free(list2);
        return head;
    }
};

另一种写法

class Partition {
public:
    ListNode* partition(ListNode* pHead, int x) {
        struct ListNode *head1,*head2,*tail1,*tail2;
        head1=tail1=head2=tail2=NULL;
        while(pHead)
        {
            if(pHead->val<x)
            {
                if(tail1==NULL)
                {
                    head1=tail1=pHead;
                }
                else
                {
                tail1->next=pHead;

                tail1=tail1->next;
                }
            }
            else
            {
                 if(tail2==NULL)
                {
                    head2=tail2=pHead;
                }
                else
                {
                tail2->next=pHead;
                tail2=tail2->next;
                }
            }
        pHead=pHead->next;
        }
        if (head1 == NULL)
        return head2;
    	if (head2 == NULL)
        return head1;
        
        tail2->next=NULL;
        tail1->next=head2;
        struct ListNode *head=head1;
        return head;
    }
};

链表的回文结构

在这里插入图片描述 在这里插入图片描述

解题思路

1.找到中间节点,然后把中间节点后面的节点进行逆序 2.没有逆序的部分与逆序的部分进行比较,相同即为回文,否则不是回文。 3.是回文结束的标准为节点为空。 4.恢复破坏的链表结构 在这里插入图片描述

代码

struct ListNode* middleNode(struct ListNode* head)
{
    struct ListNode *slow,*quick;
    slow=quick=head;
    while(head&&head->next)
    {
        slow=slow->next;
        head=head->next->next;
    }
    return slow;
}

struct ListNode* reverseList(struct ListNode* head)
{
    if(head==NULL)
    return NULL;
    struct ListNode *n1,*n2,*n3;
    n1=NULL;
    n2=head;
    n3=head->next;
    while(n2)
    {
        n2->next=n1;
        n1=n2;
        n2=n3;
        if(n3)
        n3=n3->next;
    }
    struct ListNode* newhead=n1;
    return newhead;
}

bool isPalindrome(struct ListNode* head){
    struct ListNode* mid= middleNode(head);
    mid=reverseList(mid);
    struct ListNode* temp=mid;
    struct ListNode* cur=head;
    while(cur&&mid)
    {
        if(cur->val!=mid->val)
        {
            reverseList(temp);
            return false;
        }
        
        else
        {
            cur=cur->next;
            mid=mid->next;
        }
    }
    reverseList(temp);
    return true;
}