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;
    }
};
相关推荐
不吃香菜学java3 小时前
Redis的java客户端
java·开发语言·spring boot·redis·缓存
BestOrNothing_20154 小时前
C++零基础到工程实战(4.3.3):vector数组访问与遍历
c++·迭代器·stl·vector·动态数组
贵沫末4 小时前
python——打包自己的库并安装
开发语言·windows·python
charlie1145141914 小时前
通用GUI编程技术——图形渲染实战(三十三)——Direct2D与Win32/GDI互操作:渐进迁移实战
c++·图形渲染·gui·win32
文祐4 小时前
C++类之虚函数表及其内存布局(一个子类继承一个父类)
开发语言·c++
zuowei28895 小时前
华为网络设备配置文件备份与恢复(上传、下载、导出,导入)
开发语言·华为·php
xiaohe075 小时前
超详细 Python 爬虫指南
开发语言·爬虫·python
嗑嗑嗑瓜子的猫5 小时前
Java!它值得!
java·开发语言
xiaoshuaishuai85 小时前
C# GPU算力与管理
开发语言·windows·c#
lsx2024065 小时前
SVN 创建版本库
开发语言