leetcode 133: Insertion Sort List
2013-11-19 04:18
411 查看
Insertion Sort List
Total Accepted: 1179Total Submissions: 4808
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; while(p!=NULL) { ListNode * q = head; while(q->val < p->val) { q=q->next; } if(p!=q) { int temp = q->val; q->val = p->val; while(p!=q) { q = q->next; swap(temp, q->val); } } p=p->next; } return head; } };
相关文章推荐
- [LeetCode]Insertion Sort List
- Leetcode: Insertion Sort List
- LeetCode-Insertion Sort List (Python)
- [LeetCode] Insertion Sort List 单向链表插入排序
- leetcode上的Sort a linked list using insertion sort
- LeetCode | Insertion Sort List
- leetcode 147: Insertion Sort List
- LeetCode - Insertion Sort List
- Leetcode Insertion Sort List 插入排序链表
- Leetcode155: Insertion Sort List
- leetCode(4):Insertion Sort List
- leetcode Insertion Sort List
- leetcode_num179_Insertion Sort list
- leetcode笔记--Insertion Sort List
- [Leetcode][JAVA] Insertion Sort List
- 【leetcode】Insertion Sort List
- 【LEETCODE】147- Insertion Sort List [Python]
- Insertion Sort List —— LeetCode
- [Leetcode] Insertion Sort List (Java)
- 【leetcode】Insertion Sort List