[Leedcode 86] Partition List

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 .

/*** Definition for singly-linked list. * public class ListNode {* int val; * ListNode Next; * ListNode (int X) {val = x;}}*/ Public classSolution { PublicListNode partition (ListNode head,intx) {//The first solution is very simple, the establishment of two two tables, a representative of less than X, 21 represents greater than or equal to X. //iterate through the list, add the tail of the minlist if the node value is less than X, and update the Minlist//It is important to note that the tail node of the maxlist must be empty to prevent the formation of a ring linked list        if(head==NULL)returnHead; /*ListNode minlist=new ListNode (-1);        ListNode minhead=minlist;        ListNode maxlist=new ListNode (-1);        ListNode maxhead=maxlist;                while (Head!=null) {if (head.val>=x) {maxlist.next=head;            Maxlist=head;                }else{Minlist.next=head;            Minlist=head;                    } Head=head.next;        } maxlist.next=null;        Minlist.next=maxhead.next; return minhead.next;*/                /*This solution does not meet the requirements, will destroy the original order ListNode I=head;        ListNode J=head;                while (J!=null) {if (j.val<x) {swap (I,J);                            I=i.next;                    } J=j.next; } return head;*/        //The third solution of the subject is in situ transformation. //First you need to find the first node that is greater than or equal to x and assign it to P2, while P1 points to its predecessor node//then traverse from P2 to the tail of the list, and when the value of P2 next node is less than x, you need to insert the next node of P2 into//P1 's next//The P1 maintains a tail node less than x, and the drawing is clearerListNode Newhead=NewListNode (-1); Newhead.next=Head; ListNode P1=Newhead; ListNode P2=Head; ListNode P1_rear=Head;  while(p2!=NULL&AMP;&AMP;P2.VAL&LT;X) {///////p1=P1.next; P2=P2.next; }         while(p2!=NULL&&p2.next!=NULL){            if(p2.next.val<x) {P1_rear=P1.next; P1.next=P2.next; P2.next=P2.next.next; P1.next.next=p1_rear; P1=P1.next; }Else{P2=P2.next; }        }        returnNewhead.next; }    /*Public void Swap (ListNode I,listnode j) {int temp=i.val;        I.val=j.val;    J.val=temp; }*/    }

[Leedcode 86] Partition List

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.