Leetcode 143: Analysis of Reorder list and two ways to solve the problem

Source: Internet
Author: User

Special Note: refer to a lot of predecessors of the article, organized as follows, I only did the work of re-coding, can not guarantee the best code, mainly used for communication learning.

Topic:

Method 1: Save each node address of the list in an array of pointers, and use the array to randomly access the list of adjustments.

1 structListNode2 {3     intVal;4ListNode *Next;5ListNode (intx): Val (x), Next (NULL) {}6 };7 8 voidReorderlist (ListNode *head)9 {Ten     if(Head = =nullptr) One     { A         return; -     } -  theVector<listnode *>Vecnode; -Vector<listnode *>:: Size_type left, right; -ListNode *p =head; -      while(P) +     { - Vecnode.push_back (p); +p = p->Next; A     } at  -left =0; -right = Vecnode.size ()-1; -      while(Left <Right ) -     { -Vecnode[left++]->next =Vecnode[right]; inVecnode[right--]->next =Vecnode[left]; -     } toVecnode[left]->next =nullptr; +}

Method 2: Use the speed two pointers to divide the list into two, reverse the second sub-linked list, and finally merge the two sub-lists.

1 classSolution {2  Public:3ListNode *getmid (ListNode *head)//find the midpoint of the list4     {5         if(head = = Nullptr | | head->next = =nullptr)6         {7             returnhead;8         }9 TenListNode *slow =head; OneListNode *fast = head->Next; A  -          while(Fast && fast->next) -         { theslow = slow->Next; -Fast = Fast->next->Next; -         } -         returnslow; +     } -  +ListNode *reverselist (ListNode *head)//Linked list Reverse A     { at         if(head = = Nullptr | | head->next = =nullptr) -         { -             returnhead; -         } -  -ListNode *p =head; inListNode *q = head->Next; -ListNode *tmp =nullptr; to  +          while(q) -         { theTMP = q->Next; *Q->next =p; $p =Q;Panax NotoginsengQ =tmp; -         } theHead->next =nullptr; +  A         returnp; the     } +  -ListNode *unionlist (ListNode *head1, ListNode *head2)//Merge Linked list $     { $         if(Head1 = =nullptr) -         { -             returnhead2; the         } -         if(Head2 = =nullptr)Wuyi         { the             returnHead1; -         } Wu  -ListNode *p =Head1; AboutListNode *q =head2; $ListNode *tmpp =nullptr; -ListNode *TMPQ =nullptr; -  -          while(P &&q) A         { +TMPP = p->Next; theTMPQ = q->Next; -  $P->next =Q; theQ->next =tmpp; the  thep =tmpp; theQ =TMPQ; -         } in  the         returnHead1; the     } About      the     voidReorderlist (ListNode *head)//Linked list Reflow the     { the         if(head = = Nullptr | | head->next = =nullptr) +         { -             return; the         }Bayi  theListNode *head1 =nullptr; theListNode *head2 =nullptr; -ListNode *mid =nullptr; -  theMID =Getmid (head); theHead2 = mid->Next; theMid->next = nullptr;//split into two linked lists the  -Head2 =reverselist (head2); theHead1 =head; theHead =unionlist (Head1, head2); the     }94      the};

To test your code:

Here I provide a better way, is to read the test data (multiple lines) from the file, and then reorderlist each row of data, so that it is convenient to test multiple sets of data simultaneously. The reference code is as follows:

1 voidCreateList (ListNode **head, ListNode **tail, ListNode *n)//Create a linked list2 {3     if(n! =NULL)4     {5         if(*head = =NULL)6         {7*head =N;8*tail =N;9(*tail)->next =NULL;Ten         } One         Else A         { -(*tail)->next =N; -(*tail) =N; the(*tail)->next =NULL; -         } -     } - } +  - intMainvoid) + { AIfstreaminch("Data.txt");//Open the data file at     stringLine ; -     intVal; -ListNode *head =NULL; -ListNode *tail =NULL; -ListNode *n =NULL; -  in      while(Getline (inch, line))//each time a row of data is read to line -     { to Istringstream Clin (line); +          while(Clin >> Val)//read one data at a time from each row of data -         { then =NewListNode (val); *CreateList (&head, &tail, n); $         }Panax Notoginseng  - solution List; the List.reorderlist (head); +  A          while(Head! =NULL) the         { +cout << Head->val <<' '; -Head = head->Next; $         } $cout <<Endl; -  -     } the  -     return 0;Wuyi}

The effect is as follows:

Description: by reading a lot of excellent blogs, adopt Method 2 most, simple and clear.

Leetcode 143: Analysis of Reorder list and two ways to solve the problem

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.