[LeetCode] Insertion Sort List
2014-10-15 22:46
351 查看
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) { if(head == NULL) return NULL; ListNode *dumy = new ListNode(-1); dumy->next = head; ListNode *p = head->next; head->next = NULL; while(p) { ListNode *pNext = p->next; ListNode *q = dumy; while(q->next && q->next->val < p->val) q = q->next; p->next = q->next; q->next = p; p = pNext; } p = dumy->next; delete dumy; return p; } };
相关文章推荐
- leetcode Insertion Sort List
- LeetCode:Insertion Sort List
- [Leetcode][python]Insertion Sort List
- leetcode--Insertion Sort List
- leetcode:Insertion Sort List
- LeetCode---Insertion Sort List
- Insertion Sort List[LeetCode]
- LeetCode Insertion Sort List
- [leetcode 147]Insertion Sort List
- LeetCode 147 Insertion Sort List
- LeetCode 147 Insertion Sort List(链表)
- [LeetCode] Insertion Sort List
- leetcode-Insertion Sort List
- LeetCode-Insertion Sort List (Python)
- Leetcode: Insertion Sort List
- Insertion Sort List -- LeetCode
- [leetcode]Insertion Sort List
- [leetcode] Insertion Sort List
- LeetCode Insertion Sort List 链表插入排序
- Insertion Sort List Leetcode