这里有leetcode题集分类整理!!!![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d066572249b4b144db11c32f7a40b821.png#pic_center)
考察:链表的基本操作和插入排序
AC代码:
/**
* 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 == nullptr) {
return head;
}
ListNode *prev, *curr, *next;
ListNode* dummyHead = new ListNode(0);
ListNode* lastSorted = head;
curr = head->next;
dummyHead->next = head;
while (curr != nullptr) {
if (lastSorted->val <= curr->val) {
lastSorted = lastSorted->next;
} else {
prev = dummyHead;
while (prev->next->val <= curr->val) {
prev = prev->next;
}
lastSorted->next = curr->next;
curr->next = prev->next;
prev->next = curr;
}
curr = lastSorted->next;
}
return dummyHead->next;
}
};