C++: 链表回文结构/分割链表题解

目录

1.链表的回文结构

分析

代码

2.链表分割

​编辑分析

代码


1.链表的回文结构

分析

这道题的难点是空间复杂度为O(1)

结合逆置链表+找到链表的中间节点就可以解决了。

先找到链表的中间节点,再对中间节点的下一个节点进行逆置,返回值为最后一个节点。

再把数据的两端进行比较,遇到不等的或者第二个头走到空,就结束了。全等返回真,不等返回假。

代码

cpp 复制代码
/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/

class Partition {
public:
    ListNode* partition(ListNode* pHead, int x) {
        if (pHead == nullptr || pHead->next == nullptr) {
            return pHead;
        }

        ListNode* less_head = new ListNode(0); // 头节点,小于 x 的节点链表
        ListNode* less_tail = less_head; // 尾节点,用于连接小于 x 的节点

        ListNode* greater_equal_head = new ListNode(0); // 头节点,大于等于 x 的节点链表
        ListNode* greater_equal_tail = greater_equal_head; // 尾节点,用于连接大于等于 x 的节点

        ListNode* current = pHead;

        while (current != nullptr) {
            if (current->val < x) {
                less_tail->next = current;
                less_tail = less_tail->next;
            } else {
                greater_equal_tail->next = current;
                greater_equal_tail = greater_equal_tail->next;
            }

            current = current->next;
        }

        // 将两个链表连接起来
        less_tail->next = greater_equal_head->next;
        greater_equal_tail->next = nullptr;

        // 释放多余的头节点
        ListNode* new_head = less_head->next;

        delete less_head;
        delete greater_equal_head;

        return new_head;
    }
};

2.链表分割

分析

这题难点在于很容易把它和快排联系,然后美滋滋用快慢指针法,发现一堆坑。

这题思路是分割链表,分成小节点链表和大节点链表,并且始终保证小节点的指针指向小节点链表的尾部,大节点指针指向大节点链表的尾部,再让小节点指针的尾和大节点指针的头相连。

需要注意的是,这题要改变头节点,所以要设置哨兵位。最后也要记得把大节点指针的尾连上空指针。

代码

cpp 复制代码
/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
class Partition {
public:
    ListNode* partition(ListNode* pHead, int x) {
        // write code here
        if(pHead==nullptr ||pHead->next==nullptr)
        {
            return pHead;
        }
        ListNode* cur = pHead;
        ListNode* smallhead = new ListNode(0);
        ListNode* smalltail =smallhead;
        ListNode* bighead = new ListNode(0);
        ListNode* bigtail = bighead;

        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=nullptr;
        ListNode* newhead = smallhead->next;

        delete smallhead;
        delete bighead;
        return newhead;
    }
};
相关推荐
Pniubi1 小时前
HashMap的扩容和树化边界
java·开发语言
gCode Teacher 格码致知2 小时前
Pandas教学-17:Series与Dataframe的关系
开发语言·python·pandas
发量惊人的中年网工2 小时前
分公司访问总部ERP慢怎么办?SD-WAN组网的排查与验收要点
服务器·开发语言·php
下页、再停留3 小时前
【C#桌面客户端系列学习-15】生成可视化图表之折线图、柱状图、饼状图
开发语言·c#
ShineWinsu3 小时前
对于JavaScript:操作元素的解析
开发语言·前端·javascript·面试·web·dom·元素
益莱储中国4 小时前
深度解析:RF 实验室从 5G迈向 6G 的过程中保持技术领先与设备升级的路径
开发语言·php
丰色木夕5 小时前
Ubuntu22.04下源码编译安装USD-Brep
c++·ubuntu·nvidia·usd-brep
benchmark_cc5 小时前
Python 量化系统如何设计统一的数据访问接口:从策略解耦到数据层架构
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
const_qiu6 小时前
第四章 00:开篇 —— 从「会聊天」到「记得住、会干活、能协作」
java·开发语言·人工智能·python
Wang's Blog6 小时前
Java 项目实战: 交友项目 - 个人资料管理的更新接口实现与坑
java·开发语言·交友