【LeetCode】Data Structure Problem Solution (5) [Split Linked List]

Belonging column: Playing with data structure question types
Blogger's homepage: Chuyang785
Code hosting: chuyang785
Thank you for your support, your likes and attention are the greatest support for me! ! !
Bloggers will also work harder to create better blog posts! !
Follow me, follow me, follow me, say important things three times! ! ! ! ! ! ! !

1. Source of topic

split list

2. Topic description

Given a head node head of a linked list and a specific value x, please divide the linked list so that all nodes less than x appear before nodes greater than or equal to x.
You don't need to preserve the initial relative positions of the nodes in each partition.
insert image description here

3. Problem-solving ideas

The meaning of this question is to put the data less than x on the left, and the data greater than or equal to x on the right. While changing the order, the original order is not changed.
Our idea is to create two linked lists, one linked list LessHead to store data less than x, the other linked list greaterHead to enlarge data greater than or equal to x, and finally merge the two sides into one linked list, and then return the address of the first node of the new linked list.

In order to facilitate the link, we use the method of creating a sentinel as the head node of the two linked lists. The so-called sentinel is that its val is invalid data, but its next points to the next address.
Then we create a variable cur to traverse our linked list, and then assign them to different linked lists according to their size.

Illustration:
insert image description here
when x=3, cur starts to traverse. First, the data of the first node is 1, which is smaller than 3. We link it to our LessHead linked list, and we use LessNext to record the linked nodes The address of Diandian only needs to be represented by LessHead when linking in the future. The advantage of this is that when we return the address of the head node of the linked list after the link is completed, we can directly return to LessHead.
insert image description here
After the same cur traverses the first node, it only needs to be cur=cur->next to access the second node, because we did not destroy our first node pointing to our second node when we linked the first node In the channel of the node, we can still find the second node through the first node. Similarly, we use greaterNext to represent the address of our next node.
insert image description here
By analogy, our loop stops when our cur points to NULL.
The next step is to merge the two linked lists.
insert image description here
We just need to set lessTail->next=greaterHead->next; on the line.
But there is a particularly important thing here is where does our greaterNext point after we connect? Does it really point to NULL? the answer is negative.
Let's go back to the previous link:
insert image description here
we see that our next, which stores 5 data, points to the address of 2.
insert image description here

So his image analysis becomes like this. If this is the case, it will form a loop, and there will be memory problems when returning the address.
So the solution here is to make our greaterNext->next empty, that is: greaterNext->next=NULL.

4. Code display

struct ListNode* partition(struct ListNode* head, int x)
{
    
    
    //创建哨兵位,方便链接
    struct ListNode* lessHead,*lessTail,*greaterHead,*greaterTail;
    //存放小于x的数据
    lessHead=lessTail=(struct ListNode*)malloc(sizeof(struct ListNode));
    lessHead->next=NULL;
    
	//存放大于等于x的数据
    greaterHead=greaterTail=(struct ListNode*)malloc(sizeof(struct ListNode));
    greaterHead->next=NULL;
	
	//遍历链表指针
    struct ListNode* cur=head;

    while(cur)
    {
    
    
        //尾插
        if(cur->val < x)
        {
    
    
            lessTail->next=cur;
            lessTail=cur;
        }
        else
        {
    
    
            greaterTail->next=cur;
            greaterTail=cur;
        }
        cur=cur->next;
    }
    //链接两个链表
    lessTail->next=greaterHead->next;
    //消除环,我们的5最后还指向我们2,这个时候就形成了一个环、
    greaterTail->next=NULL;

    struct ListNode* newnode=lessHead->next;
    //最后释放两个创建的内存空间
    free(lessHead);
    free(greaterHead);
    return newnode;
}

Guess you like

Origin blog.csdn.net/qq_74276498/article/details/130486885