链表问题用栈都能解决的很漂亮
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
stack<ListNode*>s1;
stack<ListNode*>s2;
ListNode *p = head;
while (p) {
if (p->val < x)s1.push(p);
else s2.push(p);
p = p->next;
}
head = NULL;
while (!s2.empty()) {
s2.top()->next = head;
head = s2.top();
s2.pop();
}
while (!s1.empty()) {
s1.top()->next = head;
head = s1.top();
s1.pop();
}
return head;
}
};