题目描述:
解题思路:
将小于x的放到一个链表中,大于x的放到另一个链表中,分别保存两个链表的头指针,最后将两个链表连接。
代码参考:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* partition(struct ListNode* head, int x){
struct ListNode* small=malloc(sizeof(struct ListNode));
struct ListNode* smallhead=small;
struct ListNode* big=malloc(sizeof(struct ListNode));
struct ListNode* bighead=big;
while(head!=NULL)
{
if(head->val<x)
{
small->next=head;
small=small->next;
}
else
{
big->next=head;
big=big->next;
}
head=head->next;
}
big->next=NULL;
small->next=bighead->next;
return smallhead->next;
}