/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *insertionSortList(ListNode *head) {
if (head == NULL )
return NULL;
ListNode *fakehead = new ListNode(0);
ListNode *next, *cur,*pre;
cur = head;
while (cur)
{
pre = fakehead;
while (pre->next != NULL && pre->next->val < cur->val)
{
pre = pre->next;
}
next = cur->next;
cur->next = pre->next;
pre->next = cur;
cur = next;
}
return fakehead->next;
}
};
【LeetCode】Insertion Sort List
最新推荐文章于 2021-12-07 09:13:28 发布