当前位置:网站首页>【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;
}
};
以上是本篇文章的全部内容,如果文章有错误或者有看不懂的地方,多和喵博主交流。互相学习互相进步。如果这篇文章对你有帮助,可以给喵博主一个关注,你们的支持是我最大的动力。
边栏推荐
- The road to systematic construction of geek planet business monitoring and alarm system
- Stochastic Adaptive Dynamics of a Simple Market as a Non-Stationary Multi-Armed Bandit Problem
- 扩展Ribbon支持基于元数据的版本管理
- 90-最近优化过的几套Oracle数据库回顾
- 阿里云视频点播播放出错,控制台访问出现code:4400
- 89-oracle SQL写法与优化器缺陷一例
- Introduction of Neural Network (BP) in Intelligent Computing
- 2022团体程序设计天梯赛L1
- Emotion analysis with RNN & CNN pytorch
- AAAI 2022 | 传统GAN修改后可解释,并保证卷积核可解释性和生成图像真实性
猜你喜欢

R language universalbank CSV "data analysis

极客星球 | 业务监控及告警系统体系化建设之路

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

慕课6、实现负载均衡-Ribbon

2022团体程序设计天梯赛L1

R language Midwest dataset visualization

Software testing - Test Case Design & detailed explanation of test classification

Introduction of neural network (BP) in Intelligent Computing

AAAI 2022 | traditional Gan can be interpreted after modification, and the interpretability of convolution kernel and the authenticity of generated image are guaranteed

Résolu: peut - on avoir plus d'une colonne auto - incrémentale dans un tableau
随机推荐
R语言AirPassengers数据集可视化
百家讲坛 雍正十三年(下部)
C语言中int和char的对应关系
A Dynamic Near-Optimal Algorithm for Online Linear Programming
百家讲坛 大秦崛起(下部)
R 语言 UniversalBank.csv“ 数据分析
pytorch的模型保存加载和继续训练
74-这类SQL优化,oracle输给了mysql,如何补救?
The road to systematic construction of geek planet business monitoring and alarm system
MySQL中如何计算同比和环比
超快变形金刚 | 用Res2Net思想和动态kernel-size再设计 ViT,超越MobileViT
Gradle Build Cache引发的Task缓存编译问题
百家讲坛 武则天
Kotlin1.6.20新功能Context Receivers使用技巧揭秘
[proteus simulation] 8x8LED dot matrix digital cyclic display
Notes d'apprentissage de golang - structure
采用网络远程访问树莓派。
极客星球 | 业务监控及告警系统体系化建设之路
Visualization of R language nutrient dataset
Remote access to raspberry pie via the Internet.