143. Reorder List

Source: Internet
Author: User

First, the topic

1, examining

  

2. Analysis

Give a list of one-way lists, reorder them, and sort the rules as above.

Second, the answer

1, Ideas:

Method One,

Watch the list of reordered links above. Can be divided into three steps.

①, find the middle node;

②, flip the linked list in the back part.

③, inserts a node in the linked list in the front part of the list.

     Public voidreorderlist (ListNode head) {if(Head = =NULL|| Head.next = =NULL)            return; ListNode Walker=Head; ListNode Runner=Head; //1. Find Mid         while(Runner.next! =NULL&& Runner.next.next! =NULL) {Walker=Walker.next; Runner=Runner.next.next; } ListNode Midnode=Walker; //2. Reverse PostListNode pre =Midnode.next; Midnode.next=NULL; ListNode Next=NULL; if(Pre.next! =NULL) Next=Pre.next; Pre.next=NULL;  while(Pre! =NULL&& Next! =NULL) {ListNode Nextandnext=Next.next; Next.next=Pre; Pre=Next; Next=Nextandnext; }                //3. Insert NodesListNode next2 =NULL;  while(Head! =NULL&& Pre! =NULL) {Next=Head.next; Next2=Pre.next; Head.next=Pre; Pre.next=Next; Head=Next; Pre=next2; }    }

143. Reorder 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.