LeetCode:Insertion Sort List
2013-11-13 22:13
357 查看
题目链接 链表的插入排序
Sort a linked list using insertion sort.
建议:为了操作方便,添加一个额外的头结点。代码如下: 本文地址
【版权声明】转载请注明出处:/article/4879640.html
Sort a linked list using insertion sort.
建议:为了操作方便,添加一个额外的头结点。代码如下: 本文地址
/** * 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) { // 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 = 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; p->next = tmp; pre->next = p; } p = pend->next; } head = pstart->next; delete pstart; return head; } };
【版权声明】转载请注明出处:/article/4879640.html
相关文章推荐
- leetcode 133: Insertion Sort List
- (leetcode)Insertion Sort List
- [leetcode147]insertion sort list
- leetcode-Insertion Sort List
- [Leetcode][python]Insertion Sort List
- Insertion Sort List (LeetCode)
- [Leetcode Solution]Insertion Sort List
- LeetCode----Insertion Sort List
- leetcode[147]Insertion Sort List
- [Leetcode] Insertion Sort List
- Insertion Sort List 【leetcode】
- leetcode Insertion Sort List(*)
- [leetcode 147] Insertion Sort List
- 【LeetCode】Insertion Sort List
- LeetCode Insertion Sort List(单链表插入排序)
- LeetCode_Insertion Sort List
- [leetcode][list][sort] Insertion Sort List
- 【LEETCODE】147- Insertion Sort List [Python]
- LeetCode147 Insertion Sort List
- 59_leetcode_Insertion Sort List