Partition List
来源:互联网 发布:工商银行数据中心待遇 编辑:程序博客网 时间:2024/06/08 11:18
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
.
设定两个头结点,一个用来连接大于等于x的结点,令一个用于连接小于x的结点,最后将大于的结点挂在小于x的结点的后面。
class Solution {public:ListNode *partition(ListNode *head, int x) {if(!head || !head->next) return head;ListNode *big = new ListNode(1);ListNode *small = new ListNode(-1);ListNode *b = big;ListNode *s = small;ListNode *p = head;while (head){ListNode *pnext = head->next;if(head->val >= x){head->next = b->next;b->next = head;b = head;}else{head->next = s->next;s->next = head;s = head;}head = pnext;}s->next = big->next;head = small->next;delete small;delete big;return head;}};
1 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
- 老船长的航海日记:ffmpeg的旅程(三)
- filter 和 servlet 中设置编码的问题
- 网络通讯合并数据发送的重要性和实现原理
- [Leetcode] Merge Intervals (Java)
- 浅谈位域,以C和C++为例
- Partition List
- ZOJ-2358
- 英语学习——————20140109
- VC中GetLastErro返回的错误码
- 一步步开始使用Discuz(1)
- 如何编写一个可靠的linux守护进程
- 使用iperf测试网络
- java代理设计模式(静态代理与动态代理)
- c#中的委托和事件