首页 > 其他 > 详细

partition-list

时间:2018-11-21 14:46:48      阅读:165      评论:0      收藏:0      [点我收藏+]

题意略:

说一下自己的两个坑点:当为空表或者只有一个节点时,应该返回head而不是NULL

#include<iostream>
using namespace std;
 struct ListNode {
     int val;
     ListNode *next;
     ListNode(int x) : val(x), next(NULL) {}
 };

class Solution {
public:
    ListNode *partition(ListNode *head, int x) {
        if (head == NULL||head->next==NULL)return head;
        ListNode *head1 = new ListNode(-1);
        ListNode *head2 = new ListNode(-1);
        ListNode *end1 = head1;
        ListNode *end2 = head2;
        ListNode *p = head;
        while (p){
            if (p->val < x){
                end1 = end1->next = p;
            }
            else{
                end2 = end2->next = p;
            }
            p = p->next;
        }
        end2->next = NULL;
        end1->next = head2->next;
        return head1->next;
    }
};

 

partition-list

原文:https://www.cnblogs.com/ALINGMAOMAO/p/9994698.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!