单链表算法 - 链表分割

链表分割_牛客题霸_牛客网现有一链表的头指针 ListNode* pHead,给一定值x,编写一段代码将所有小于x的。题目来自【牛客题霸】https://www.nowcoder.com/practice/0e27e0b064de4eacac178676ef9c9d70思路:

代码:

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
        //创建两个非空链表:小链表和大链表
        ListNode* lesstHead,*lesstTail;
        lesstHead = lesstTail = (ListNode*)malloc(sizeof(ListNode));

        ListNode* greaterHead,*greaterTail;
        greaterHead = greaterTail = (ListNode*)malloc(sizeof(ListNode));
        //创建临时变量来遍历原数组
        ListNode* prev = pHead;
        while(prev)
        {
            //判断当前节点是否小于x
            if(prev->val < x)
            {
                //插入到小链表中
                lesstTail->next = prev;
                lesstTail = lesstTail->next;
            }
            else
            {
                //插入到大链表中
                greaterTail->next = prev;
                greaterTail = greaterTail->next;
            }
            prev = prev->next;
        }
        //退出循环原链表遍历完成
        //连接两个链表
        lesstTail->next = greaterHead->next;
        ListNode* ret = lesstHead->next;
        free(lesstHead);
        lesstHead = NULL;
        free(greaterHead);
        greaterHead = NULL;
        return ret;
    }
};

提交结果:

当我们提交代码之后代码有问题,那么代码到底哪里的逻辑不合适呢?我们画图看一下。

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
        //创建两个非空链表:小链表和大链表
        ListNode* lesstHead,*lesstTail;
        lesstHead = lesstTail = (ListNode*)malloc(sizeof(ListNode));

        ListNode* greaterHead,*greaterTail;
        greaterHead = greaterTail = (ListNode*)malloc(sizeof(ListNode));
        //创建临时变量来遍历原数组
        ListNode* prev = pHead;
        while(prev)
        {
            //判断当前节点是否小于x
            if(prev->val < x)
            {
                //插入到小链表中
                lesstTail->next = prev;
                lesstTail = lesstTail->next;
            }
            else
            {
                //插入到大链表中
                greaterTail->next = prev;
                greaterTail = greaterTail->next;
            }
            prev = prev->next;
        }
        //将大链表的尾节点的next指针置为NULL
        greaterTail->next = NULL;
        //连接两个链表
        lesstTail->next = greaterHead->next;
        ListNode* ret = lesstHead->next;
        free(lesstHead);
        lesstHead = NULL;
        free(greaterHead);
        greaterHead = NULL;
        return ret;
    }
};

提交结果:

相关推荐
郝学胜-神的一滴44 分钟前
Effective STL 条款09:慎重选择删除元素的方法
开发语言·网络·数据结构·c++·算法·stl
FlightYe3 小时前
linux系统编程(二十一):C 语言常见坑
android·linux·运维·服务器·c语言·音视频
geovindu5 小时前
python:Logistic Regression
开发语言·后端·python·算法
番茄味城市甜菜6 小时前
二叉树刷题(C语言)
c语言·开发语言
geovindu6 小时前
python: Random Forest
开发语言·后端·python·算法·随机森林
.道阻且长.7 小时前
Linux基础IO:理解文件以及回顾C文件接口
linux·运维·服务器·c语言·开发语言·职场和发展
derek19977 小时前
软考软件设计师:主键、传递依赖和冗余,三问其实是一件事
c语言·c++·计算机网络·软件工程·学习方法
凌冰_7 小时前
数据结构 - 线性结构(三)
java·数据结构·算法·软件工程
拂拉氏7 小时前
【知识讲解】 数据结构与算法-树状数组的认识
数据结构·算法·树状数组
djjjx.8 小时前
Mysql数据库基础篇(十一):索引的常见操作、聚簇与非聚簇索引、为什么其他数据结构不能作为索引的底层结构
数据结构