Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
For example,
Given 1->4->3->2->5->2
and x = 3,
return 1->2->2->4->3->5
.
Subscribe to see which companies asked this question
/**
* 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 smaller_head(0);
ListNode larger_head(0);
ListNode *smaller_tail = &smaller_head;
ListNode *larger_tail = &larger_head;
while (head != NULL) {
if (head->val < x) {
smaller_tail->next = head;
smaller_tail = head;
}
else {
larger_tail->next = head;
larger_tail = head;
}
head = head->next;
}
smaller_tail->next = larger_head.next;
larger_tail->next = NULL;
return smaller_head.next;
}
};