86. 分隔链表
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* partition(ListNode* head, int x) { ListNode *mirror, *mirror1, *small, *large; mirror = new ListNode(0); mirror1 = new ListNode(-1); small = mirror; large = mirror1; while(head) { if(head->val<x) { small->next = head; small = small->next; head = head->next; } else { large->next = head; large = large->next; head = head->next; } } large->next = NULL; small->next = mirror1->next; return mirror->next; } };
原文:https://www.cnblogs.com/jessica216/p/13388402.html