题目链接
https://leetcode.com/problems/partition-list/
代码
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode *lt = NULL, *lth = NULL, *gt = NULL, *gth = NULL;
while (head != NULL) {
if (head->val < x) {
if (lt != NULL) {
lt->next = head;
} else {
lth = head;
}
lt = head;
} else {
if (gt != NULL) {
gt->next = head;
} else {
gth = head;
}
gt = head;
}
head = head->next;
}
if (lth == NULL) {
return gth;
} else {
lt->next = gth;
if (gt != NULL) {
gt->next = NULL;
}
return lth;
}
}
};
网友评论