[Leetcode]89. Partition List Chain List division

Source: Internet
Author: User

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

You should preserve the original relative order of the nodes in each of the.

For example,
Given 1->4->3->2->5->2 and x = 3,
Return 1->2->2->4->3->5 .

Subscribe to see which companies asked this question

Solution 1: From the beginning, find the first value greater than or equal to the X node, and then find the first node after the node is less than X, and finally the small node inserted in front of the large node, and then move to the first large node in the previous step, continue to loop the previous processing, until there is no value less than x node.
/** Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x) : Val (x), Next (NULL) {}}; */classSolution { Public: ListNode* Partition (listnode* head,intx) {if(head = = NULL | | head->next = = NULL)returnHead; ListNode*help =NewListNode (0); Help->next =Head; ListNode* prev = help, *curr = prev->Next;  while(Curr! =NULL) {             while(Curr! = NULL && Curr->val < x) {//find the first node greater than or equal to xCurr = curr->Next; Prev= prev->Next; }            if(Curr = = NULL) Break; ListNode*next = curr->next, *temp = Curr;//note not starting with the first node             while(Next! = NULL && next->val >= x) {//find the first node less than xNext = next->Next; Temp= temp->Next; }            if(Next = NULL) Break; ListNode* tmp = next->Next; Next->next = prev->next;//insert nodes that are less than x before nodes greater than or equal to xPrev->next =Next; Temp->next =tmp; Prev= Next;//move forward to the next nodeCurr = prev->Next; }        returnHelp->Next; }};

Actually the first while loop in the outer while loop can get out of the outer while loop, because the first node that is found next to the value greater than or equal to X is Curr itself.

/** Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x) : Val (x), Next (NULL) {}}; */classSolution { Public: ListNode* Partition (listnode* head,intx) {if(head = = NULL | | head->next = = NULL)returnHead; ListNode*help =NewListNode (0); Help->next =Head; ListNode* prev =Help ;  while(Prev->next! = NULL && prev->next->val < x) prev = prev->Next; ListNode* Curr =prev;  while(Curr->next! =NULL) {            if(Curr->next! = NULL && curr->next->val >=x) Curr= curr->Next; Else{ListNode* tmp = curr->Next; Curr->next = tmp->Next; TMP->next = prev->Next; Prev->next =tmp; Prev= prev->Next; }        }        returnHelp->Next; }};

Solution 2: Another way is to iterate through the linked list, respectively, the value is less than x and the value is greater than or equal to the X node is divided into two chain links, and then link the latter at the end of the former.

[Leetcode]89. Partition List Chain List division

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.