插入排序

    1. /**
    2. * Definition for singly-linked list.
    3. * struct ListNode {
    4. * int val;
    5. * ListNode *next;
    6. * ListNode() : val(0), next(nullptr) {}
    7. * ListNode(int x) : val(x), next(nullptr) {}
    8. * ListNode(int x, ListNode *next) : val(x), next(next) {}
    9. * };
    10. */
    11. class Solution {
    12. public:
    13. ListNode* insertionSortList(ListNode* head) {
    14. if(head==nullptr||head->next==nullptr){
    15. return head;
    16. }
    17. ListNode* head1=head;
    18. ListNode* head2=head;
    19. while(head->next!=nullptr){
    20. head=head->next;
    21. while(head1!=head){
    22. if(head1->val>head->val){
    23. int temp=head1->val;
    24. head1->val=head->val;
    25. head->val=temp;
    26. }
    27. head1=head1->next;
    28. }
    29. head1=head2;
    30. }
    31. return head2;
    32. }
    33. };