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;
    }
};
相关推荐
利刃大大1 分钟前
【动态规划:01背包】01背包详解 && 模板题 && 优化
c++·算法·动态规划·力扣·背包问题
9ilk11 分钟前
【基于one-loop-per-thread的高并发服务器】--- 前置技术
运维·服务器·c++·笔记·后端·中间件
千里镜宵烛1 小时前
深入 Lua 环境机制:全局变量的 “容器” 与 “隔离术”
开发语言·junit·lua
苏比的博客2 小时前
Windows MFC添加类,变量,类导向
c++·windows·mfc
yudiandian20142 小时前
MFC - 使用 Base64 对图片进行加密解密
c++·mfc
yudiandian20142 小时前
MFC - Picture Control 控件显示图片
c++·mfc
QX_hao7 小时前
【Go】--反射(reflect)的使用
开发语言·后端·golang
inferno7 小时前
Maven基础(二)
java·开发语言·maven
我是李武涯7 小时前
从`std::mutex`到`std::lock_guard`与`std::unique_lock`的演进之路
开发语言·c++
卡提西亚8 小时前
C++笔记-10-循环语句
c++·笔记·算法