【leetcode】Partition List
2015-05-18 13:18
375 查看
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.
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.
class Solution { public: ListNode* partition(ListNode* head, int x) { if(head==NULL) return NULL; ListNode * less=new ListNode(0); ListNode * greater=new ListNode(0); ListNode * greaterhead=greater; ListNode * lesshead=less; ListNode * p=head; while(p) { if(p->val<x) { less->next=p; p=p->next; less=less->next; less->next=NULL; } else { greater->next=p; p=p->next; greater=greater->next; greater->next=NULL; } } less->next=greaterhead->next; return lesshead->next; } };
相关文章推荐
- [LeetCode] Partition List 划分链表
- LeetCode:Partition List
- Leetcode 86 Partition List
- LeetCode | Partition List(partition方法处理链表)
- Partition List | LeetCode 8ms C++Solution
- [Leetcode]Partition List
- LeetCode-Partition List
- LeetCode-- Partition List
- LeetCode: Partition List
- [Leetcode][python]Partition List
- [leetcode]Partition List
- [Leetcode 71] 86 Partition List
- [leetcode]Partition List
- leetcode[86]:Partition List
- [LeetCode] Partition List
- 【LeetCode】Partition List
- LeetCode 86 Partition List (Python详解及实现)
- [LeetCode]Partition List
- leetcode——86——Partition List
- [LeetCode-86] Partition List (链表数据分区)