leetcode_147 Insertion Sort List
来源:互联网 发布:上海编程培训机构 编辑:程序博客网 时间:2024/05/21 10:22
题目分析:
- 链表的插入排序实现。
解题思路:
两种方法实现
遍历实现
1)利用四个节点实现,其中一个节点指向当前带插入节点,一个节点指向带插入节点的下一个节点,一个节点记录待插入链表实际该插入位置的前一个元素,一个节点指向其带插入链表该插入位置的下一个节点;
2)遍历链表,每次先记录当前带插入节点的下一个节点,然后从有序链表的表头开始遍历查找,当出现逆序时,将此节点插入即可。
实现程序
//对链表执行插入排序struct ListNode *insertionSortList(struct ListNode *head){ if (head == NULL) return NULL; if (head->next == NULL) return head; //建立一个虚初始节点 struct ListNode *temp = (struct ListNode *) malloc (sizeof(struct ListNode)); temp->next = head; struct ListNode *p = head->next; head->next = NULL; struct ListNode *r; struct ListNode *h1; struct ListNode *h2; while (p != NULL) { //记录下一个元素 r = p->next; h1 = temp->next; h2 = temp; //从前向后扫描比较进行插入 while (h1 != NULL) { //出现逆序,进行插入 if (h1->val > p->val) { h2->next = p; p->next = h1; break; } else { h1 = h1->next; h2 = h2->next; } } if (h1 == NULL) { h2->next = p; p->next = NULL; } p = r; } return temp->next;}
0 0
- leetcode_147 Insertion Sort List
- Insertion Sort List | leetcode
- Leetcode: Insertion Sort List
- [LeetCode] Insertion Sort List
- Insertion Sort List
- Leetcode: Insertion Sort List
- [LeeCode] Insertion Sort List
- leetcode-Insertion Sort List
- [LeetCode]Insertion Sort List
- Insertion Sort List
- 【LeetCode】Insertion Sort List
- LeetCode | Insertion Sort List
- LeetCode - Insertion Sort List
- Insertion Sort List
- Insertion Sort List
- Insertion Sort List
- Insertion Sort List
- [LeetCode] Insertion Sort List
- Spring
- 03-Eclipse中maven的helloWorld
- Android调试,The selected device is incompatible
- windows内核情景分析笔记------内核对于物理页面的管理
- Android 中Fragment与Activity通信大汇总
- leetcode_147 Insertion Sort List
- ubuntu server 改变phpmyadmin的默认访问路径
- scala学习资料
- 快速排序
- DatePickerDialog日期选择器的使用
- 制作一个简单的猜数字游戏
- 仿百度壁纸客户端(二)——主页自定义ViewPager广告定时轮播图
- 【Codeforces659D】Bicycle Race【向量叉积】
- 1年半没上班后又被扔去做后台的全职妈妈