【leetcode】【单链表】【86】Partition List
来源:互联网 发布:微信小视频插图软件 编辑:程序博客网 时间:2024/06/13 06:25
#include<iostream>using namespace std;struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};class Solution {public:ListNode* partition(ListNode* head, int x) {if (head == NULL || head->next == NULL)return head;ListNode* cur = head;ListNode* last = head;while (cur){//寻找第一个小于x的节点if (cur->val >= x){last = cur;//记录最后一个大于等于x的节点cur = cur->next;}elsebreak;}if (cur == NULL) //所有节点的值都大于等于xreturn head;ListNode* first = head;//插入点的前一点head = cur;//第一个小于x的节点就是头结点if (head != first){//说明第一个小于x的节点前面有节点大于等于xlast->next = head->next;head->next = first;}first = head;//插入点的前一点cur = last->next;while (cur){ //我用的方法是一个节点一个节点的插入,而没有用小于x的[first,last)内的节点整体插入if (cur->val < x){if (first==last){//前面的节点的值都是小于xfirst = last = cur;cur = cur->next;}else{ //有节点的值大于等于xlast->next = cur->next;cur->next = first->next;first->next = cur;first = cur;cur = last->next;}}else{cur = cur->next;last = last->next;}}return head;}ListNode* createList(ListNode* head){int numOfNode;int value;cout << "please input number of listNode:";cin >> numOfNode;cin >> value;head = new ListNode(value);ListNode* cur = head;for (int i = 1; i < numOfNode; ++i){cin >> value;ListNode* temp = new ListNode(value);cur->next = temp;cur = temp;}return head;}void printNode(ListNode* head){ListNode* cur = head;while (cur){cout << cur->val << " ";cur = cur->next;}cout << endl;}};int main(){ListNode* head = NULL;Solution lst;head = lst.createList(head);lst.printNode(head);head = lst.partition(head, 4);lst.printNode(head);system("pause");return 0;}
0 0
- 【leetcode】【单链表】【86】Partition List
- LeetCode 86: Partition List
- LeetCode 86 Partition List
- [leetcode 86] Partition List
- leetcode-86 Partition List
- [LeetCode 86]Partition List
- leetcode || 86、Partition List
- LeetCode---(86) Partition List
- LeetCode 86 Partition List
- Leetcode[86]-Partition List
- leetcode[86]:Partition List
- Leetcode #86 Partition List
- LeetCode(86) Partition List
- leetcode 86: Partition List
- [Leetcode]#86 Partition List
- leetCode #86 Partition List
- leetcode 86:Partition List
- 【leetcode】【86】Partition List
- UVA 10465 - Homer Simpson(贪心-考察代码能力哦)
- 第十二周项目四 点、圆的关系-(3)求点与圆心的连线在圆上的交点
- 狂欢过后是孤单
- 素数打表
- 微观SOA:服务设计原则及其实践方式(上篇)
- 【leetcode】【单链表】【86】Partition List
- hdu 2578
- 我对java简单认识
- C#单例模式的三种写法
- 购物车功能抛物线轨迹
- ref和out的使用与区别
- 第十一周 阅读项目 4 问题二 构造函数调用的先后问题系统会自动帮你调整但是不提倡。
- hdu 2899(方程题,二分查找)
- 浅谈模式识别中的特征提取