二分查找树的知识点,链表可以快速变化大小,却不可以快速查找,除了数组可以快速二分查找以外,另一个就是二分查找树。
链表排序:
冒泡排序,交换链表的值
class Solution {
public: ListNode *bubbleSortList(ListNode *head) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. //链表快速排序 if(head == NULL || head->next == NULL)return head; ListNode *p = NULL; bool isChange = true; while(p != head->next && isChange)//初始值p为空,然后从最后一个不断向前进 { ListNode *q = head; isChange = false;//标志当前这一轮中又没有发生元素交换,如果没有则表示数组已经有序 for(; q->next && q->next != p; q = q->next) { if(q->val > q->next->val) { swap(q->val, q->next->val); isChange = true; } } p = q; } return head; }};
浙公网安备 33010602011771号