有大佬可以看看我的代码为什么会段错误
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};*/
class Partition {
public:
ListNode* partition(ListNode* pHead, int x)
{
// write code here
ListNode* newhead=pHead,*tail=NULL,*head=NULL;
ListNode* less=NULL,*greater=NULL;
while(newhead)
{
if(newhead->val<x)
{
if(less==NULL)
{
less=tail=newhead;
}
tail->next=newhead;
tail=newhead;
}
else
{
if(greater==NULL)
{
greater=head=newhead;
}
head->next=newhead;
head=newhead;
}
newhead=newhead->next;
}
tail->next=greater;
head->next=NULL;
return less;
}
};
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};*/
class Partition {
public:
ListNode* partition(ListNode* pHead, int x)
{
// write code here
ListNode* newhead=pHead,*tail=NULL,*head=NULL;
ListNode* less=NULL,*greater=NULL;
while(newhead)
{
if(newhead->val<x)
{
if(less==NULL)
{
less=tail=newhead;
}
tail->next=newhead;
tail=newhead;
}
else
{
if(greater==NULL)
{
greater=head=newhead;
}
head->next=newhead;
head=newhead;
}
newhead=newhead->next;
}
tail->next=greater;
head->next=NULL;
return less;
}
};
全部评论
报的啥错
中科曙光?
相关推荐
点赞 评论 收藏
分享
查看9道真题和解析 点赞 评论 收藏
分享
查看21道真题和解析 点赞 评论 收藏
分享


