1. 程式人生 > >partition-list

partition-list

一個 esp 坑點 name nod lse space part clu

題意略:

說一下自己的兩個坑點:當為空表或者只有一個節點時,應該返回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