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)