一つのペアは、トピックのタイトルの意味を理解することはできません。
リストのヘッドを用いることを理解すべき基準値よりも小さい----補間 - 間違っ
いいえ文法問題は、論理的な主題の意味を理解せず、IDEでテストすることはできません
#include<stdio.h>
struct ListNode* partition(struct ListNode* head, int x);
/*
编写程序以 x 为基准分割链表,使得所有小于 x 的节点排在大于或等于 x 的
节点之前。如果链表中包含 x,x 只需出现在小于 x 的元素之前(如下所示)。
分割元素 x 只需处于“右半部分”即可,其不需要被置于左右两部分之间。
示例 :
输入 : head = 3->5->8->5->10->2->1, x = 5
输出 : 3->1->2->10->5->5->8
*/
typedef struct ListNode {
int val;
struct ListNode *next;
};
void main(){
struct ListNode *head;
head = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode *node1;
node1 = (struct ListNode*)malloc(sizeof(struct ListNode));
int value = 2;
node1->val = value;
head->next = node1;
node1->next = NULL;
struct ListNode *node2;
node2 = (struct ListNode*)malloc(sizeof(struct ListNode));
value = 1;
node2->val = value;
node1->next = node2;
node2->next = NULL;
struct ListNode *node3;
node3 = (struct ListNode*)malloc(sizeof(struct ListNode));
value = 1;
node3->val = value;
node2->next = node3;
node3->next = NULL;
struct ListNode *node4;
node4 = (struct ListNode*)malloc(sizeof(struct ListNode));
value = 3;
node4->val = value;
node3->next = node4;
node4->next = NULL;
head = partition(head, 2);
while (head->next != NULL){
printf("%d", head->next->val);
head = head->next;
// printf("hello");
}
system("pause");
}
//不需要保证分割节点不要保证左边小右边大
struct ListNode* partition(struct ListNode* head, int x){
//根据传入的参数 节点加上 分割点
struct ListNode *pre;
struct ListNode *post;
post = head;
if (head == NULL){
return head;
}
pre = post->next;
int mark = 0;
while (pre != NULL){
if (pre->val >= x){
mark = 1;
}
if (mark == 1 && pre->val < x){
post->next = pre->next;
pre->next = head->next;
head->next = pre;
pre = post->next;
}
else{
pre = pre->next;
post = post->next;
}
// printf("%d", post->val);
}
return head;
}