链表分割
来源:互联网 发布:捷速pdf文字识别软件 编辑:程序博客网 时间:2024/06/06 19:29
题目描述
编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前
给定一个链表的头指针 ListNode* pHead,请返回重新排列后的链表的头指针。注意:分割以后保持原来的数据顺序不变。
--------------------------------------------------------------
扫描一遍链表,将小于x的节点和不小于x的节点分别添加到一个新建链表中,最后做连接。
/*struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) {}};*/class Partition {public: ListNode* partition(ListNode* pHead, int x) { ListNode* lshead = nullptr; ListNode* lstail = nullptr; ListNode* gthead = nullptr; ListNode* gttail = nullptr; ListNode* pcur = nullptr; while (pHead){ pcur = pHead->next; pHead->next = nullptr; if (pHead->val < x){ if (!lshead){ lshead = pHead; lstail = pHead; }else { lstail->next = pHead; lstail = pHead; } }else { if (!gthead){ gthead = pHead; gttail = pHead; }else { gttail->next = pHead; gttail = pHead; } } pHead = pcur; } if (gttail) gttail->next = nullptr; if (lstail) lstail->next = gthead; else lshead = gthead; return lshead; }};
0 0
- 分割单向链表
- 分割链表
- 链表分割
- 链表分割
- 链表分割(Java)
- 链表分割
- 牛客网 | 链表分割
- 链表分割
- 链表分割
- 链表分割
- 链表分割问题
- 链表分割
- 链表分割
- 链表分割
- 链表分割
- 链表分割
- [牛客]链表分割
- 链表分割
- Dynomite
- JAVA/iOS/Android/PHP 通用DES 加密
- ios linphone-iphone编译
- ES节点类型以及各种节点的分工:
- Kaggle练习赛Titanic手札
- 链表分割
- 公司用到的一些 iOS 开源库和第三方组件
- Android文件图片上传的详细讲解(一)HTTP multipart/form-data 上传报文格式实现手机端上传
- make
- angularjs学习笔记 ng-class
- ssi服务器端指令详解(shtml)
- 反射机制读取自定义注解
- 阅读郭林《第一行代码》的笔记——第13章 继续进阶,你还应该掌握的高级技巧
- iOS 简单封装的一个夜间模式