[LeetCode] Insertion Sort List
2015-02-09 12:06
309 查看
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){return NULL;} ListNode *p=new ListNode(0); p->next = head; head = p; ListNode *q; while (p->next!=NULL){ q = head; bool flag = false; while (q!=p){ if (q->next->val>p->next->val){ ListNode* tmp1=p->next; p->next =p->next->next; tmp1->next = q->next; q->next = tmp1; flag =true; break; }else{ q=q->next; } } if (!flag){ p=p->next; } } return head->next; } };
相关文章推荐
- leetcode insertion-sort-list
- leetcode Insertion Sort List(链表插入排序)
- LeetCode :: Insertion Sort List [具体分析]
- LeetCode Insertion Sort List
- The Solution to LeetCode 147 Insertion Sort List
- Leetcode[147]-Insertion Sort List
- leetcode-Insertion Sort List
- 【Leetcode】Insertion Sort List (Sorting)
- 【LeetCode】Insertion Sort List 解题报告
- leetcode day2 -- Sort List && Insertion Sort List
- Leetcode: Insertion Sort List
- LeetCode-Insertion Sort List
- LeetCode 147:Insertion Sort List
- Leetcode---Insertion Sort List
- [LeetCode-Algorithms-147] "Insertion Sort List" (2017.12.21-WEEK16)
- Leetcode Insertion Sort List
- 【leetcode】 Insertion_Sort_List
- leetCode解题报告之Insertion Sort List
- [leetcode]Insertion Sort List
- LeetCode: Insertion Sort List(外加对单链表类题目的思考总结)