The linked list is adjusted to be less than or equal to K in front, greater than k in the rear

Source: Internet
Author: User

For a linked list, we need to use a specific threshold to complete its differentiation, so that the node is less than or equal to the value of the previous, the node is greater than the value of the next, while ensuring that the two types of nodes within the position of the same relationship.

Given a list of head node heads, at the same time given a threshold Val, return a linked list so that the node is less than or equal to its first, greater than or equal to its after, guaranteed node value is not duplicated.

Test examples:
{1,4,2,5},3
{1,2,4,5}
ImportJava.util.*;/*Public class ListNode {int val;    ListNode next = null;    ListNode (int val) {this.val = val; }}*/ Public classDivide { PublicListNode Listdivide (ListNode head,intval) {ListNode Onehead=NULL;//used to hold the head of a linked list that is less than or equal to the given value ListNode onetail=NULL;//used to hold the end of a linked list that is larger than the given value. * * Must remember to write this tail, or every time you have to traverse to find the tail trouble dead ListNode twohead=NULL;//Ibid. to hold ListNode Twotail greater than the given value part=NULL; ListNode Next=NULLThis is used to hold the head's next node while(head!=NULL) {Next=head.next;//These two steps must be noted, but also the most error-prone place, our goal is to get the value inside the head into their new linked list, so each time the head of the next clear null,
So each head is a pure head, so directly to the ListNode type of data structure directly assigned to the target list of the next field can be. Also think of is Val and next separate assignment, but they need to be included in a complete listnode inside, so very troublesome//this next is to be saved for the next round into while, otherwise you give Head.next assignment is null, You can't find me behind this list.
Head.next=NULL; if(head.val<=val) {//If the part is less than equalsif(onehead==NULL) {//Determine if the current head node is empty, Onehead=head;//If the current head node is empty, it means that the value is the first to be added and set to the head node Onetail=head;//at the same time there is only one, then he is also the tail node}Else{//If not the first to come in Onetail.next=head;//this time to make the current list of the next point to the current Onetail=head;//also let Onetail point to the newly added tail}} if(head.val>val) {//Ibid .if(twohead==NULL) {Twohead=Head; Twotail=Head; }Else{Twotail.next=Head; Twotail=Head; }} head=next;//the part of the head that has just been stored in next, and re-passes it to the head for the next round of the while loop}if(onehead!=NULL{//To determine if Onehead is empty (it is possible that the values in the list are greater than the given value), Onetail.next=twohead;//is not empty, indicating that Onehead has next field, can be connected, there is no need to consider twohead situation, because even if twohead is null, also does not affect Onetail point to him}Else{Onehead=twohead;//If Onehead is empty, it means that an element is not (because the first one is set to Onehead), this time should be returned directly twohead, but in order to facilitate the consent of the output, he was assigned to Onehead,} returnOnehead; }}

Places to be aware of:

1. Set head and tail two variables in their newly created list to save heads and tails, respectively.

2. When assigning a node to next on another node, clean up the next node

3. Be sure to update the tail pointer in real-time when adding tail nodes behind a linked list

The linked list is adjusted to be less than or equal to K in front, greater than k in the rear

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.