有大佬可以看看我的代码为什么会段错误

/*
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;
    }
};
全部评论
报的啥错
点赞 回复 分享
发布于 2023-09-04 16:08 广东
中科曙光?
点赞 回复 分享
发布于 2023-08-17 13:32 上海

相关推荐

评论
2
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务