****(leetcode)Insertion Sort List
2014-06-16 18:20
274 查看
/** * 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 || head->next == NULL)return head; ListNode *p = head->next, *pstart = new ListNode(0), *pend = head; pstart->next = head; //为了操作方便,添加一个头结点 while(p != NULL) { ListNode *tmp = pstart->next, *pre = pstart; while(tmp != p && p->val >= tmp->val) {tmp = tmp->next; pre = pre->next;} if(tmp == p)pend = p; else { pend->next = p->next;//pend为p节点前面一个节点,确保p节点插入后,链表仍然连续 p->next = tmp; pre->next = p; } p = pend->next; } head = pstart->next; delete pstart; return head; } };
不常看连插入排序都忘了!!!
相关文章推荐
- [leetcode] Insertion Sort List(python)
- 【LeetCode】Insertion Sort List
- leetcode---Insertion Sort List
- 59_leetcode_Insertion Sort List
- LeetCode——Insertion Sort List
- [leetcode]Insertion Sort List
- Leetcode Insertion Sort List
- (leetcode)Insertion Sort List
- LeetCode Insertion Sort List
- [LeetCode]Insertion Sort List
- Leetcode Insertion Sort List 解题报告
- leetcode:Insertion Sort List
- Insertion Sort List 【leetcode】
- leetcode Insertion Sort List(*)
- LeetCode题解:Insertion Sort List
- 【LeetCode】Insertion Sort List
- Leetcode: Insertion Sort List
- [LeetCode] Insertion Sort List
- LeetCode--insertion-sort-list
- 【leetcode】【单链表】【147】Insertion Sort List