Leetcode 143. Reorder List

Source: Internet
Author: User

VER0:

The simplest idea is to get the middle node, put the node after it into the stack, and start with the head and insert the Stack.top ().

Specific explanation: After getting slow, in two cases, the list length is even, the slow is the last node of the first half; the list length is odd, and the slow is the previous node of the middle node. In both cases the Reoder Slow->next is still in its original position, so mid = Slow->next->next.

72ms

1 /**2 * Definition for singly-linked list.3 * struct ListNode {4 * int val;5 * ListNode *next;6 * ListNode (int x): Val (x), Next (NULL) {}7  * };8  */9 classSolution {Ten  Public: One     voidReorderlist (listnode*head) { A         if(head = = NULL | | head->next = = NULL)return; -listnode* slow = head, * fast = head->Next; -          while(Fast->next && fast->next->next) { theslow = slow->Next; -Fast = Fast->next->Next; -         } -listnode* mid = Slow->next->Next; +Slow->next->next =NULL; -Stack<listnode*>Stk; +          while(mid) { A Stk.push (mid); atMID = Mid->Next; -         } -listnode* Resh =head; -          while(!Stk.empty ()) { -listnode* tmp = resh->Next; -Resh->next =stk.top (); inResh->next->next =tmp; - Stk.pop (); toResh =tmp; +         } -          the          *     } $};

All two of the following algorithms are 64ms

Ver1:

Put all the nodes into the vector, the variables I, J start at the ends of the vector, link.

1 classSolution {2  Public:3     voidReorderlist (listnode*head) {4Vector<listnode*>v;5listnode* tmp =head;6          while(TMP) {7 V.push_back (TMP);8TMP = tmp->Next;9         }       Ten          for(intI=0, J=v.size ()-1; i<=j; i++, j-- ) { One             if(tmp) Tmp->next =V[i]; AV[i]->next =V[j]; -TMP =V[j]; -         } the         if(tmp) Tmp->next =NULL; -     } -};

Ver2:

Get the midpoint of the list, reverse the second half of the list, and then merge the two-segment linked list.

1 classSolution {2  Public:3listnode* Reverselist (listnode*head) {4     if(Head==null | | head->next==null)returnhead;5ListNode *pre=head, *cur=pre->next, *Post;6Pre->next =NULL;7      while(cur!=NULL) {8Post = cur->Next;9Cur->next =Pre;TenPre =cur; OneCur =Post; A     } -     returnPre; - } the voidMergelist (listnode* L1, ListNode *L2) { -ListNode *cur1=l1, *cur2=l2,*post1,*Post2; -      while(cur2!=NULL) { -Post1 = cur1->Next; +Post2 = cur2->Next; -Cur1->next =Cur2; +Cur2->next =Post1; ACur1 =Post1; atCUR2 =Post2; -     } - } - voidReorderlist (listnode*head) { -     if(Head==null | | head->next==null | | head->next->next==null)return; -ListNode *slow=head,*fast=head; in      while(Fast->next!=null && fast->next->next!=NULL) { -Fast = Fast->next->Next; toslow = slow->Next; +     } -ListNode *head2 = slow->Next; theSlow->next =NULL; * mergelist (Head,reverselist (head2)); $ }Panax Notoginseng};

There is also a recursive algorithm that does not look, leaving the pit

Https://leetcode.com/discuss/93197/concise-recursive-o-solution-with-space-easy-to-understand

Leetcode 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.