2.2.3—单链表— Partition List
来源:互联网 发布:java map 泛型定义 编辑:程序博客网 时间:2024/06/04 19:32
描述
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater
than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater
than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
For example, Given 1->4->3->2->5->2 and x = 3, return 1->2->2->4->3->5.
#include<iostream>using namespace std;struct node{int data;node *next;};class mylist{node *head;public:mylist(){head = new node();head->next = NULL;}void CreateList(int a[], int n);void Display();friend void PartionList(mylist &list, int value);~mylist();};void mylist::CreateList(int a[], int n){node *p = head;for (int i = 0; i < n; i++){node *q = new node();q->data = a[i];p->next = q;p = q;}p->next = NULL;}void mylist::Display(){node *p = head->next;while (p){cout << p->data << " ";p = p->next;}cout << endl;}void PartionList(mylist &list, int value){node *pre = list.head;node *p = list.head->next;//===while (p){if (p->data < value){pre = pre->next;p = p->next;}else{break;}}//===while (p){node *temp = p;while (p&&p->data >= value)//注意先后顺序{temp = p;p = p->next;}if (p){temp->next = p->next;p->next = pre->next;pre->next = p;pre = p;p = temp;}}}mylist::~mylist(){node *p = head;while (head){head = p->next;delete p;p = head;}}int main(){//===const int n = 8;int a[n] = { 1, 4, 3, 2, 5, 2,8,6 };//代表417593mylist list;list.CreateList(a, n);list.Display();//===int value = 4;PartionList(list, value);list.Display();}
阅读全文
0 0
- 2.2.3—单链表— Partition List
- 2.2.3 Partition List
- 2.2.3 Partition List
- LeetCode86——Partition List
- 链表——partition list
- Algorithms—86.Partition List
- leetcode 086 —— Partition List
- Leetcode——86. Partition List
- 算法系列——Partition List
- LeetCode 之 Partition List — C++ 实现
- [LeetCode] Partition List 分割单链表
- 【leetcode】【单链表】【86】Partition List
- leetcode:单链表之Partition List
- leetcode——86——Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- html5 video调用servlet播放
- 专题:网络流问题
- 专题:博弈论
- mybatis不区分大小写问题
- STL :sscanf sprintf的应用
- 2.2.3—单链表— Partition List
- PAT 数列的片段和【简单数论】
- 微信小程序开发深入解读
- 日志输出两次 spring初始化两次
- PAT 天梯赛真题集(L2、L3)
- 用Unity 3D生成真实地形
- Codeforces Round #404 (Div. 2)
- 你应该选择哪一个Web前端框架?,选Vue还是React?
- 基于物品的协同过滤-电影推荐