【LeetCode】86. Separated linked list

 

Table of contents

1. Introduction to the topic

2. Problem-solving ideas

2.1. Double linked list double pointers

2.2. Code description


 

1. Introduction to the topic

Original question link:86. Separated linked list - LeetCode

 Example 1:

Import:head = [1,4,3,2,5,2], x = 3

Output:[1,2,2,4,3,5]

 Example 2:

Import:head = [2,1], x = 2

Output:[1,2]

 hint:

  • The number of nodes in the linked list is in the range [0, 200]
  • -100 <= Node.val <= 100
  • -200 <= x <= 200

2. Problem-solving ideas

According to the meaning of the question, consider dividing the original linked list by "creating two new linked lists". The algorithm process is:

  1. Create two new linked listssmall  and BigEqu , respectively Used to link nodes less than the number of signs x and greater than or equal to the number of signs x node.   
  2. Traverse the linked list head and compare the node values ​​​​in sequence head->val and x The size of a>, add the node pointed by head to the linked list< /span>At the end. BigEqu , then add the node pointed by head to the linked listhead->val >= x at the end. If small head->val < x , if
  3. After completing the passage, connectsmall BigEqu table .   
  4. Finally judge the head node and return.

2.1. Double linked list double pointers

First compare head->val and x, and find that head->val is smaller than x at this time, so it is put into the small linked list. At this time, the chain head of small is smallsmallH< /span>. head points to the next node. node 1 both point to smallT and chain tail

Continue to compare head->val and x, and find that head->val is greater than x at this time, so it is put into the BigEqu linked list. At this time, the chain head of BigEqu isBigEquH< /span>. head points to the next node. node 4 both point toBigEquT and chain tail

Continue to compare head->val and x, and find that head->val is equal to x at this time, so it is put into the BigEqu linked list. At this time, the end of the BigEqu chainBigEquT< /span>. head points to the next node. node 3 points to

Continue to compare head->val and x, and find that head->va is smaller than x at this time, so it is put into the small linked list. At this time, the tail of the small chain issmallT< /span>. head points to the next node. node 2 points to

Continue to compare head->val and x, and find that head->va is greater than x at this time, so it is put into the BigEqu linked list. At this time, the tail of the BigEqu chainBigEquT< /span>. head points to the next node. node 5 points to

Continue to compare head->val and x, and find that head->va is smaller than x at this time, so it is put into the small linked list. At this time, the tail of the small chain issmallT< /span>. head points to the next node. If head is null at this time, the loop stops. node 2 points to

At this time, the linked list with smallH as the head is The set of nodes less than the number of signs x, and the linked list with BigEqu as the head is The set of nodes greater than or equal to the number of signs x, you only need to point the next of smallT at the end of the small linked list to the head of the BigEqu linked list, and finally return small Chain head smallT  is enough.  

2.2. Code description

The process of loop is relatively easy to understand, but special circumstances need to be considered when merging linked lists at the end. If there are no nodes smaller than the number of signs, the link head returned at this time is not smallH, but the link head of BigEqu. BigEquH.

struct ListNode* partition(struct ListNode* head, int x){
    struct ListNode* smallH = NULL; //小于头
    struct ListNode* smallT = NULL; //小于尾
    struct ListNode* BigEquH = NULL; //大于等于头
    struct ListNode* BigEquT = NULL; //大于等于尾
    struct ListNode* next = NULL;
    //小于连一起,大于等于连一起
    while(head)
    {
        next = head->next;  //用next保存head的next,然后将head->置为空
        head->next = NULL; //确保此时的head的next置为空,不然可能会导致死循环报错
        if(head->val < x)  //小于标志数
        {
            if(smallH == NULL)   //等于NULL表示第一次放入结点,
                                    //此时链头链尾都指向同一个结点
            {
                smallH = smallT = head;  
            }
            else    //small的链尾的next指向head
            {
                smallT->next = head;
                smallT = head;
            }
        }
        else  //大于等于标志数
        {
            if(BigEquH == NULL)  //同理
            {
                BigEquH = BigEquT = head;
            }
            else
            {
                BigEquT->next = head;
                BigEquT = head;
            }
        }
        head = next;   //head指向下一个结点
    }

    //判断是否可以尾接头
    if(smallT != NULL)  //当small链尾不为空,即small链表有结点时,
                        //才让small链尾连接BigEqu链头
    {
        smallT->next = BigEquH;
    }
    
    return smallH == NULL ? BigEquH : smallH;  //当small链表没有结点时,返回链头BigEquH,
                                                //否则返回链头smallH
}

More [LeetCode quizzes] recommendations:

[LeetCode] LCR170 Use the idea of ​​​​merge sort to solve the reverse order problem (detailed illustration)_Hacynn's blog-CSDN blogicon-default.png?t=N7T8https://blog.csdn.net/zzzzzhxxx /article/details/133578735[LeetCode] 75 sub-process partition of quick sort (Dutch flag problem) - CSDN Blogicon-default.png?t=N7T8https://blog. csdn.net/zzzzzhxxx/article/details/133785886[LeetCode] 297. Serialization and Deserialization of Binary Trees - CSDN Blogicon-default.png?t=N7T8https:/ /blog.csdn.net/zzzzzhxxx/article/details/133827375

 

If you think the author's writing is good, please give the blogger a big like and support. Your support is my biggest motivation for updating!

If you think the author's writing is good, please give the blogger a big like and support. Your support is my biggest motivation for updating!

If you think the author's writing is good, please give the blogger a big like and support. Your support is my biggest motivation for updating!

Guess you like

Origin blog.csdn.net/zzzzzhxxx/article/details/133942678