LeetCode - Partition List
2014-09-16 14:57
323 查看
题意
传入一个链表的头指针和一个数,把小于这个数的都放前面,大于的位置不变接着放后面。
思路
用了非常2的方法。开两个队列,分别存放大于的和小于的,再开一个队列一个个存放指针,最后拿出来接上去。。
代码
class Solution {
queue<int> qsml, qbig;
queue<ListNode *> p;
public:
ListNode *partition(ListNode *head, int x) {
if (!head) return head;
do
{
p.push(head);
if (head->val >= x) qbig.push(head->val);
else qsml.push(head->val);
head = head->next;
}while (head);
while (!p.empty())
{
ListNode *cur = p.front(); p.pop();
if (!head)
head = cur;
if (!qsml.empty())
{
cur->val = qsml.front();
qsml.pop();
}
else
{
cur->val = qbig.front();
qbig.pop();
}
if (!p.empty()) cur->next = p.front();
}
return head;
}
};
相关文章推荐
- LeetCode Partition List
- [LeetCode] Partition List 分割单链表
- [leetcode] Partition List
- LeetCode 86 Partition List (Python详解及实现)
- leetcode 086 —— Partition List
- 【Leetcode 86】 Partition List
- leetcode 86: Partition List
- 【leetcode】Partition List
- LeetCode Partition List
- LeetCode No.86 Partition List
- LeetCode题解-86-Partition List
- [LeetCode] Partition List
- LeetCode: Partition List
- LeetCode Partition List
- LeetCode:Partition List
- Leetcode: Partition List
- 【LeetCode with Python】 Partition List
- [LeetCode刷题记录]Partition List
- [Leetcode] Partition list 划分链表
- leetcode - Partition List