#include <stdio.h>
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* partition(struct ListNode* head, int x)
{
if(head==NULL)
{
return head;
}
//定义两个头节点,分别存储小于和大于的值
struct ListNode *less=malloc(sizeof(struct ListNode));
struct ListNode *more=malloc(sizeof(struct ListNode));
less->next=NULL;
more->next=NULL;
struct ListNode *cur=head,*curLess=less,*curMore=more;
while(cur!=NULL)
{
//值小于x,接在less节点后面
if(cur->val<x)
{
curLess->next=cur;
curLess=cur;
}
else
//值大于等于x,接在more节点后面
{
curMore->next=cur;
curMore=cur;
}
//不要忘了移动节点
cur=cur->next;
}
//拼接两个大小链表
curLess->next=more->next;
curMore->next=NULL;
return less->next;
}
01-18
573

08-04
1367
