leetcode Insertion Sort List(链表插入排序)
2014-04-02 11:14
543 查看
题目大意:
对一个单向链表进行插入排序。
c++代码:
对一个单向链表进行插入排序。
c++代码:
class Solution { public: ListNode *insertionSortList(ListNode *head) { if(head == NULL || head->next == NULL) return head; ListNode* begin = head; ListNode* pre = head; ListNode* cur = head->next; begin->next = NULL; ListNode* insert_pos = begin; while(cur != NULL) { pre = begin; insert_pos = begin; //find the insert position while(insert_pos != NULL) { if(insert_pos->val > cur->val) break; pre = insert_pos; insert_pos = insert_pos->next; } //tmp is the node which will insert to sort list ListNode* tmp = cur; cur = cur->next; //if it is the end of the sort list if(insert_pos == NULL) { pre->next =tmp; pre->next->next = NULL; } else { //if the sort list just have one node if(insert_pos == pre) { tmp->next = insert_pos; begin = tmp; } else// the postion is in the middle of sort list { pre->next = tmp; tmp->next = insert_pos; } } } return head = begin;//reset head pointer to begin } };
相关文章推荐
- LeetCode 147 Insertion Sort List(链表插入排序)
- [C++]LeetCode: 126 Insertion Sort List (插入排序链表)
- Leetcode Insertion Sort List 插入排序链表
- leetcode——Insertion Sort List 对链表进行插入排序(AC)
- LeetCode Insertion Sort List 链表的插入排序
- LeetCode Insertion Sort List(单链表插入排序)
- [LeetCode147]Insertion Sort List(链表插入排序)
- LeetCode:Insertion Sort List//链表插入排序
- 【LeetCode-面试算法经典-Java实现】【147-Insertion Sort List(链表插入排序)】
- LeetCode147_Insertion Sort List(用插入排序算法对链表进行排序) Java题解
- LeetCode 147 Insertion Sort List(链表)
- LeetCode Insertion Sort List 链表插入排序
- leetcode---insertion-sort-list---链表
- 链表的插入排序 Insertion Sort List
- LeetCode | Insertion Sort List(插入法排序链表)
- 173. 链表插入排序 (insertion-sort-list)(c++)----lintcode面试题之链表
- Insertion Sort List(单链表插入排序)
- leetcode 链表 Insertion Sort List
- leetcode_147题——Insertion Sort List(线性表,插入排序)
- 【leetcode】【单链表】【147】Insertion Sort List