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