简体   繁体   中英

Partition the list

ListNode* partition(ListNode* head, int x) {
    ListNode *p1 = NULL, *p2 = NULL, *cur = head, *cur1, *cur2;
    while (cur != NULL) {
        if (cur->val < x) {
            if (p1 == NULL) {
                p1 = cur;
                cur1 = cur;
            } else {
                cur1->next = cur;
                cur1 = cur1->next;
            }
        }
        else {
            if (p2 == NULL) {
                p2 = cur;
                cur2 = cur;
            } else {
                cur2->next = cur;
                cur2 = cur2->next;
            }
        }
        cur = cur->next;
    }
    if (cur2 != NULL) cur2->next = NULL;
    if (p1 != NULL && cur1 != NULL) {
        cur1->next = p2;
        return p1;
    } 
    return p2;
}

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

But I got an error for some test case:

  • Runtime Error Message:

free(): invalid next size (fast): 0x00000000020f1200 ***

  • Last executed input:

     [-29,-2,-43,-26,-12,58,-55,-44,-75,83,9,46,29,-6,-79,-73,-96,66,66,-100,20,-89,-14,-67,12,96,27,-36,29,-68,29,52,30,38,79,70,-3,-76,-73,-26,60,-12,-80,-11,82,94,16,86,12,-17,39,-68,-54,-75,-82,58,74,-3,74,-45,29,-45,59,-89,94,38,82,-57,15,-91,-31,-25,-3,-58,64,-69,-64,-20,69,0,15,8,32,61,-14,2,-29,-88,-100,97,-81,29,-47,30,-7,99,-31,-73,94,36,88,-37,-89,-63,5] 

    105

Your function is correct. the error exists somewhere else in your code. You should search for the memory error message..

I briefly implemented the following code:

#include <iostream>

using namespace std;

struct ListNode{
    int val;
    ListNode* next;
};


void insert(ListNode *head, int x){
    ListNode *cur = head;
    ListNode *new_node = new ListNode;
    new_node->val = x;
    new_node->next = 0;
    while(cur->next != 0){
        cur = cur->next;
    }
    cur->next = new_node;
}

void print(ListNode *head){
    ListNode *cur = head;
    while (cur != 0){
        cout << cur->val << "-";
        cur = cur->next;
    }
    cout << "NULL" << endl;
}
ListNode* partition(ListNode* head, int x);

int main(){
    ListNode* head = new ListNode;
    head->val= 1;
    head->next = 0;
    insert(head, 4);
    insert(head, 3);
    insert(head, 2);
    insert(head, 5);
    insert(head, 2);
    print(head);
    ListNode * newhead = partition(head, 3);
    cout << endl;
    print(newhead);
    return 0;
}


ListNode* partition(ListNode* head, int x) {
    ListNode *p1 = NULL, *p2 = NULL, *cur = head, *cur1, *cur2;
    while (cur != NULL) {
        if (cur->val < x) {
            if (p1 == NULL) {
                p1 = cur;
                cur1 = cur;
            } else {
                cur1->next = cur;
                cur1 = cur1->next;
            }
        }
        else {
            if (p2 == NULL) {
                p2 = cur;
                cur2 = cur;
            } else {
                cur2->next = cur;
                cur2 = cur2->next;
            }
        }
        cur = cur->next;
    }
    if (cur2 != NULL) cur2->next = NULL;
    if (p1 != NULL && cur1 != NULL) {
        cur1->next = p2;
        return p1;
    } 
    return p2;
}

it gives 1-4-3-2-5-2 and the partitioned list 1-2-2-4-3-5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM