Q86: Partition List
分析
算法
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode* l1 = new ListNode(0);
ListNode* l2 = new ListNode(0);
ListNode* l1_2 = l1;
ListNode* l2_2 = l2;
while(head){
if(head->val >= x){
l2_2 -> next = head;
l2_2 = l2_2->next;
}else{
l1_2 ->next = head;
l1_2 = l1_2->next;
}
head = head->next;
}
l2_2->next = NULL;
l1_2->next = l2->next;
return l1->next;
}
};Last updated