leetcode_147 title--insertion sort list (linear table, insert sort)

Source: Internet
Author: User

Insertion Sort ListTotal accepted:40386 Total submissions:154512my submissions QuestionSolution

Sort a linked list using insertion sort.

Hide TagsLinked List SortHas you met this question in a real interview? Yes No

Discuss

    This question has nothing to say, Mainly to investigate the operation and insertion sort of the linear table

#include <iostream>using namespace std;struct listnode {int val;     ListNode *next; ListNode (int x): Val (x), Next (NULL) {}}; listnode* insertionsortlist (listnode* head) {listnode* ptr=head; listnode* Ptr_start; listnode* Ptr_end; listnode* temp; listnode* cur; listnode* Pre; listnode* pre1;ptr_start=ptr_end=ptr;if (Ptr==null) return ptr;if (ptr->next==null) return ptr;ptr_start=ptr;ptr_ End=ptr->next;if (Ptr_start->val>=ptr_end->val) {ptr_start->next=ptr_end->next;ptr_end-> Next=ptr_start;temp=ptr_start;ptr_start=ptr_end;ptr_end=temp;ptr=ptr_start;} Cur=ptr_end->next;while (cur) {pre=ptr_start;pre1=ptr_start->next;if (cur->val<=pre->val) {ptr_end- >next=cur->next;cur->next=ptr_start;ptr_start=cur;ptr=ptr_start;cur=ptr_end->next;} Else{int Flag=0;while (pre1!=cur) {if (cur->val<=pre1->val) {ptr_end->next=cur->next;cur->next= Pre1;pre->next=cur;flag=1;cur=ptr_end->next;break;} Else{pre1=pre1->next;pre=pre->next;}} IF (FLAg==0) {Ptr_end=ptr_end->next;cur=cur->next;}}} return ptr;} int main () {listnode* head;head= (listnode*) malloc (sizeof (ListNode));head->val=2; listnode* ptr2;ptr2= (listnode*) malloc (sizeof (ListNode)); head->next=ptr2;head->next->next=null;ptr2- >val=1; listnode* ptr3;ptr3= (listnode*) malloc (sizeof (ListNode));head->next->next=ptr3;ptr3->val=4; listnode* ptr4;ptr4= (listnode*) malloc (sizeof (ListNode));head->next->next->next=ptr4;ptr4->val=3; ptr4->next=null;cout<

  

leetcode_147 title--insertion sort list (linear table, insert sort)

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.