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: OneListNode *insertionsortlist (ListNode *head) { AListNode *list =head; - - if(List = = NULL | | list->next = =NULL) the { - returnlist; - } - +ListNode *P1 =list; -ListNode *P2 = p1->Next; +ListNode *P3 = p2->Next; A at if(P1->val > p2->val) - { -Swap (P1->val, p2->val); - } - - while(P3! =NULL) in { -ListNode *temp = p3->Next; to + if(P1->val > p3->val) - { theListNode *tmp =P3; * while(P2->next! =p3) $P2 = p2->Next;Panax NotoginsengP2->next = p3->Next; -Tmp->next =P1; thelist=tmp; +P1 =list; AP2 = p1->Next; theP3 =temp; + } - Else $ { $ while(P2->val < p3->val) - { -P1 = p1->Next; theP2 = p2->Next; - }Wuyi if(P2! =p3) the { -P1->next =P3; WuListNode *tmp =P2; - while(Tmp->next! =p3) AboutTMP = tmp->Next; $Tmp->next = p3->Next; -P3->next =P2; -P1 =list; -P2 = list->Next; AP3 =temp; + } the Else - { $P1 =list; theP2 = list->Next; theP3 =temp; the } the - } in the } the returnlist; About the } the};
Single-linked list insertion sort