Question: (96) Partition List
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->null and x = 3,
return 1->2->2->4->3->5->null.
题解:
依据题意,是要根据值x对链表进行分割操作,具体是指将所有小于x的节点放到不小于x的节点之前,咋一看和快速排序的分割有些类似,但是这个题的不同之处在于只要求将小于x的节点放到前面,而并不要求对元素进行排序。
这种分割的题使用两路指针即可轻松解决。左边指针指向小于x的节点,右边指针指向不小于x的节点。由于头节点不确定,我们可以使用dummy节点这个大杀器。
/**
* Definition of ListNode
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param head: The first node of linked list.
* @param x: an integer
* @return: a ListNode
*/
ListNode *partition(ListNode *head, int x) {
if (NULL == head) {
return NULL;
}
ListNode *leftDummy = new ListNode(0);
ListNode *rightDummy = new ListNode(0);
ListNode *left = leftDummy;
ListNode *right = rightDummy;
while (head != NULL) {
if (head->val < x) {
left->next = head;
left = head;
} else {
right->next = head;
right = head;
}
head = head->next;
}
right->next = NULL;
left->next = rightDummy->next;
return leftDummy->next;
}
};
源码分析:
right->next
为空,将右链表的头部链接到左链表尾指针的next,返回左链表的头部