题解 | #链表分割#

链表分割

https://www.nowcoder.com/practice/0e27e0b064de4eacac178676ef9c9d70

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
class Partition {
public:
    ListNode* partition(ListNode* pHead, int x) 
    {
        struct ListNode* newhead, *newtail,*oldhead, *oldtail;
        newhead = newtail = (struct ListNode*)malloc(sizeof(struct ListNode));
        oldhead = oldtail = (struct ListNode*)malloc(sizeof(struct ListNode));
        struct ListNode* cur = pHead;

        while(cur)
        {
            if(cur->val < x)
            {
                newtail->next = cur;
                newtail = cur;
                cur = cur->next;
            }
            else 
            {
                oldtail->next = cur;
                oldtail = cur;
                cur = cur->next;
            }

        }

        newtail->next = oldhead->next;
        oldtail->next = NULL;

        return newhead->next;
    }
};











全部评论

相关推荐

点赞 评论 收藏
分享
ohs的小木屋:比不少实习待遇高了
点赞 评论 收藏
分享
05-11 20:45
门头沟学院 Java
有担当的灰太狼又在摸...:零帧起手查看图片
点赞 评论 收藏
分享
能干的三文鱼刷了10...:公司可能有弄嵌入式需要会画pcb的需求,而且pcb能快速直观看出一个人某方面的实力。看看是否有面试资格。问你问题也能ai出来,pcb这东西能作假概率不高
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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