Partition List
来源:互联网 发布:淘宝双11红包口令 编辑:程序博客网 时间:2024/05/16 19:47
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
.
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *partition(ListNode *head, int x) { vector<int> a,b; ListNode *p,*s,*l; p=head; while(p!=NULL){ if(p->val<x){ a.push_back(p->val); p=p->next; } else{ b.push_back(p->val); p=p->next; } } l=(ListNode*)malloc(sizeof(ListNode)); p=l; for(int i=0;i<a.size();i++){ s=(ListNode*)malloc(sizeof(ListNode)); s->val=a[i]; p->next=s; p=s; } for(int i=0;i<b.size();i++){ s=(ListNode*)malloc(sizeof(ListNode)); s->val=b[i]; p->next=s; p=s; } p->next=NULL; l=l->next; return l; }};
0 0
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- 关系型数据库 V.S. 非关系型数据库
- C语言及程序设计提高例程-19 越界的后果
- Spring定时任务的几种实现
- android:使用fragment实现tab切换
- (二)Maven学习笔记
- Partition List
- Tesseract-OCR 样本训练,生成语言文件
- C语言及程序设计提高例程-25 指针作函数参数
- C语言及程序设计提高例程-24 数组名作为函数参数
- C语言及程序设计提高例程-23 指针的运算
- C语言及程序设计提高例程-22 用指针法访问数组元素
- C语言及程序设计提高例程-21 首尝排序——冒个泡
- C语言及程序设计提高例程-20 经典:查找的艺术
- linux中socket的连接数