I. Description of the topic
Given a singly linked list l: l0→l1→ ... →ln-1→lN,
Reorder it to: l0→ln→l1→ln-1→l2→L n-2→ ...
You must does this in-place without altering the nodes ' values.
For example,
Given {1,2,3,4}
, reorder it to {1,4,2,3}
.
Second, analysis
1. Violence solution
The time required for this solution is relatively high in complexity, O (N2)
The code is as follows: the code submitted on Leetcode will prompt for timeouts
Public voidreorderlist (ListNode head) {if(head==NULL|| head.next==NULL|| head.next.next==NULL){//when the number of nodes is less than or equal to 2 o'clock, no action is required. return ; } ListNode Rearnode=NULL;//this pointer points to the tail node of the linked listListNode CurrentNode =head;//the front node has been plugged in.ListNode prenode=NULL;//always point to the front node of the Rearnode node . while(currentnode!=NULL) {Rearnode=CurrentNode; while(rearnode.next!=NULL){//look for the tail knot.Prenode=Rearnode; Rearnode=Rearnode.next; } if(Rearnode!=currentnode) {//when the Rearnode is equal to the CurrentNode node, the expression is closed .prenode.next=NULL; Rearnode.next=Currentnode.next; Currentnode.next=Rearnode; CurrentNode=Rearnode.next; } Else{ Break; } } }
2, time faster solution, the algorithm is mainly divided into three parts:
A, looking for the middle node of the list (Midnode), and divides the linked list into two, the head node of a list of heads and Newhead respectively;
b, the chain list newhead to reverse;
C, the reversal of the linked list is inserted into the list of the head list.
D, time complexity of O (n)
The code is implemented as follows:
PackageCom.edu.leetcode;ImportCom.edu.leetcode.*; Public classReorderlist { Public voidreorderlist (ListNode head) {if(head==NULL|| head.next==NULL|| head.next.next==NULL){//when the number of nodes is less than or equal to 2 o'clock, no action is required. return ; } /** The first part is mainly used to find the middle node of the linked list.*/ListNode Midnode=head;//finding the middle node of a linked listListNode Rearnode=head.next;//Midnode Take a step, ReadNode walk two steps while(rearnode!=NULL) {Rearnode=Rearnode.next; if(rearnode!=NULL) {Midnode=Midnode.next; Rearnode=Rearnode.next; } } /** The second part is to divide the list into two parts and reverse the list behind it.*/ListNode Newhead=Midnode.next; Midnode.next=NULL; ListNode Curentnode=newhead;//This node is used to point to the first node and never needs to move the position .Rearnode=curentnode.next;//a node behind the CurrentNode. while(rearnode!=NULL){//Place a node behind CurrentNode in front of the head node (newhead)curentnode.next=Rearnode.next; Rearnode.next=Newhead; Newhead=Rearnode; Rearnode=Curentnode.next; } /** The third part inserts the list of Newhead as the head node into the head linked list.*/Curentnode=head;//the position currently inserted in the headRearnode=newhead;//the current newhead node . while(curentnode!=NULL&&rearnode!=NULL){//Insert the Rearnode node behind the CurentnodeNewhead=rearnode.next;//re-assigning Newhead to a valuerearnode.next=Curentnode.next; Curentnode.next=Rearnode; Curentnode=Rearnode.next; Rearnode=Newhead; } } Public Static voidMain (string[] args) {//TODO auto-generated Method StubListNode First1 =NewListNode (0); ListNode rear1=First1; for(inti=1;i<10;i++) {ListNode Q=NewListNode (i); Rear1.next=Q; Rear1=Q; } ListNode Q=First1; while(q!=NULL) {System.out.print (Q.val+ ","); Q=Q.next; } System.out.println (); Reorderlist RL=Newreorderlist (); Rl.reorderlist (FIRST1); ListNode P=First1; while(p!=NULL) {System.out.print (P.val+ ","); P=P.next; } System.out.println (); }}
"Leetcode" Reorder List JAVA