typedef struct ListNode ListNode;
ListNode* partition(ListNode* phead, int x) {
ListNode* lessHead, * lessTail;
ListNode* greaterHead, * greaterTail;
lessHead = lessTail = (ListNode*)malloc(sizeof(ListNode));
greaterHead = greaterTail = (ListNode*)malloc(sizeof(ListNode));
lessHead->next = greaterHead->next = NULL;
ListNode* cur = phead;
while (cur) {
if (cur->val < x) {
lessTail->next = cur;
lessTail = lessTail->next;
}
else {
greaterTail->next = cur;
greaterTail = greaterTail->next;
}
cur = cur->next;
}
lessTail->next = greaterHead->next;//连接
greaterTail->next = NULL;//将greater置空,防止存放后面的地址,形成环
ListNode* list = lessHead->next;//删除哨兵
free(greaterHead);
free(lessHead);
return list;
}
【数据结构】【链表代码】链表分割
最新推荐文章于 2024-11-10 22:49:00 发布