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;
    }
};
相关推荐
iCxhust6 小时前
c# U盘映像生成工具
开发语言·单片机·c#
许小燚7 小时前
线性表——双向链表
数据结构·链表
yangzhi_emo7 小时前
ES6笔记2
开发语言·前端·javascript
emplace_back8 小时前
C# 集合表达式和展开运算符 (..) 详解
开发语言·windows·c#
jz_ddk8 小时前
[学习] C语言数学库函数背后的故事:`double erf(double x)`
c语言·开发语言·学习
萧曵 丶8 小时前
Rust 所有权系统:深入浅出指南
开发语言·后端·rust
xiaolang_8616_wjl8 小时前
c++文字游戏_闯关打怪2.0(开源)
开发语言·c++·开源
夜月yeyue8 小时前
设计模式分析
linux·c++·stm32·单片机·嵌入式硬件
收破烂的小熊猫~8 小时前
《Java修仙传:从凡胎到码帝》第四章:设计模式破万法
java·开发语言·设计模式