当前位置:网站首页>【CM11 链表分割】
【CM11 链表分割】
2022-06-22 19:41:00 【爱吃榴莲的喵星人】
一、题目描述
现有一链表的头指针 ListNode pHead,给一定值x,编写一段代码将所有小于x的结点排在其余结点之前,且不能改变原来的数据顺序,返回重新排列后的链表的头指针。
二、提供方便走读代码的图

三、题目代码
提示:注意死循环,一定要把bigTail->next置NULL
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) {} };*/
class Partition {
public:
ListNode* partition(ListNode* pHead, int x) {
ListNode* smallHead=(ListNode*)malloc(sizeof( ListNode));
smallHead->next=NULL;
ListNode* bigHead=(ListNode*)malloc(sizeof( ListNode));
bigHead->next=NULL;
ListNode* cur,*bigTail,*smallTail;
cur=pHead;
smallTail=smallHead;
bigTail=bigHead;
while(cur)
{
if(cur->val<x)
{
smallTail->next=cur;
smallTail=smallTail->next;
}
else
{
bigTail->next=cur;
bigTail=bigTail->next;
}
cur=cur->next;
}
bigTail->next=NULL;
smallTail->next=bigHead->next;
ListNode* newHead=smallHead->next;
free(smallHead);
free(bigHead);
return newHead;
}
};
以上是本篇文章的全部内容,如果文章有错误或者有看不懂的地方,多和喵博主交流。互相学习互相进步。如果这篇文章对你有帮助,可以给喵博主一个关注,你们的支持是我最大的动力。
边栏推荐
- 软件测试——测试用例设计&测试分类详解
- Scheduling with Testing
- 评估指标及代码实现(NDCG)
- 如何使用Feign构造多参数的请求
- uniapp小程序商城开发thinkphp6积分商城、团购、秒杀 封装APP
- Unityeditor editor script execution menu
- R语言organdata 数据集可视化
- Ultrafast transformers | redesign vit with res2net idea and dynamic kernel size, surpassing mobilevit
- A Dynamic Near-Optimal Algorithm for Online Linear Programming
- R 语言USArrests 数据集可视化
猜你喜欢

R language universalbank CSV "data analysis

从感知机到Transformer,一文概述深度学习简史

Visualization of wine datasets in R language

Easyclick fixed status log window

底部菜单添加的链接无法跳转到二级页面的问题

laravel+宝塔计划任务

Easydss problem and solution summary

MySQL advanced (II)

One picture decoding opencloudos community open day

R 语言nutrient数据集的可视化
随机推荐
72-最近一次现场生产系统优化的成果与开发建议
山东大学科技文献期末复习(个人速成向)
Implementation of UART with analog serial port
Emotion analysis with RNN & CNN pytorch
采用网络远程访问树莓派。
R语言penguins数据集可视化
How to use feign to construct multi parameter requests
Cross domain cors/options
慕课5、服务发现-Nacos
真正的缓存之王Caffine Cache
Golang學習筆記—結構體
R语言midwest数据集可视化
Ribbon负载均衡
性能测试(一)
怎样实现网页端im即时通讯中的@人功能
从感知机到Transformer,一文概述深度学习简史
How to realize @ person function in IM instant messaging
laravel+宝塔计划任务
MySQL中如何计算同比和环比
Agricultural futures account opening