"Disclaimer: All rights reserved, please indicate the source of the reprint, do not use for commercial purposes. Contact mailbox: [Email protected] "
Title Link: https://leetcode.com/problems/insertion-sort-list/
Test instructions
Given a linked list, requires that a list of sorted lists be returned using an insert sort
Ideas:
Create a new linked list, follow the Insert Sort feature, each time the new linked list finds the location where the new node will be inserted
/** * Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * listnode (int x): Val (x), Next (NULL) {} *}; */class Soluti on{public:listnode* insertionsortlist (listnode* head) {if (Head==nullptr | | head->next==nullptr) return head; ListNode *newlist = new ListNode (0); ListNode *cur = head;while (cur) {ListNode *ptr = newlist; ListNode *pnext = Cur->next;while (ptr && ptr->next && ptr->next->val<cur->val) {ptr = Ptr->next;} Cur->next = Ptr->next;ptr->next = Cur;cur = Pnext;} Return newlist->next;}};
Copyright NOTICE: This article is the original blogger article, if reproduced, please indicate the source
[Leedcode OJ] #147 insertion Sort List