[Data Structure OJ Question] Linked List Segmentation

Original link: https://www.nowcoder.com/practice/0e27e0b064de4eacac178676ef9c9d70?tpId=8&&tqId=11004&rp=2&ru=/activity/oj&qru=/ta/cracking-the-coding-interview/question-ranking

Table of contents

1. Description of the topic

2. Thinking analysis

3. Code implementation


1. Description of the topic

2. Thinking analysis

The overall idea: Create two linked lists , store nodes less than x and nodes greater than or equal to x respectively , and perform tail insertion respectively .

It will be easier to use the sentinel position for this question , for the following reasons ( it can avoid many empty situations ):

(1) Using the sentinel bit does not need to consider the situation that the two linked lists are empty when the end is inserted.

(2) There is no need to consider whether it is empty when the two linked lists are linked.

(3) Even if one linked list is empty, there is still a head node of the sentinel position, and the normal link is enough.

We use structure pointers lhead and ltail to represent the linked list whose value is less than x , and use structure pointers ghead and gtail to represent the linked list whose value is greater than or equal to x.

Use the malloc() function to apply for two nodes, that is, the sentinel positions of the two linked lists, so that lhead and ltail point to one of them at the beginning, and ghead and gtail point to the other at the beginning.

Then create a structure pointer cur to traverse the original linked list. We use a while loop to traverse the nodes when cur is not empty.

When the value of the node is less than x , we tail-insert this node into the first linked list ( ltail->next=cur ). Then let ltai go one step backward ( ltai=ltail->next ).

When the value of the node is greater than or equal to x , we insert the tail of the node into the second linked list ( gtail->next=cur ). Then let gtail go one step backward ( gtail=gtail->next ).

After inserting a node , let cur take a step back ( cur=cur->next ). Stop the loop when cur is empty .

Then link the two linked lists . ( ltail->next=ghead->next ).

There is one thing that requires great attention! ! !

Set gtail->next=NULL

Otherwise rings may appear.

Because lhead is now pointing to the sentinel bit, we need to move lhead one step back ( lhead=lhead->next ).

Because we are afraid that the next position of lhead cannot be found, we introduce a structure pointer head to save the next position of lhead . ( struct ListNode *head=lhead->next ).

Then in order to prevent memory leaks, we need to use free() to release the two sentinel bits (ie free(lhed) and free(ghead) ).

Finally, just return to the head .

3. Code implementation

/*
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 *lhead,*ltail,*ghead,*gtail;
    lhead=ltail=(struct ListNode*)malloc(sizeof(struct ListNode));
    ghead=gtail=(struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode *cur=pHead;
    while(cur)
    {
        if(cur->val<x)
        {
            ltail->next=cur;
            ltail=ltail->next;
        }
        else
        {
            gtail->next=cur;
            gtail=gtail->next;
        }
        cur=cur->next;
    }
    ltail->next=ghead->next;
    //不空,可能导致出现环
    gtail->next=NULL;
    struct ListNode *head=lhead->next;
    free(lhead);
    free(ghead);
    return head;
    }
};

Guess you like

Origin blog.csdn.net/m0_62531913/article/details/132349028