LeetCode147: Insertion Sort List
2015-05-04 19:24
399 查看
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) { ListNode tmpNode = ListNode(INT_MIN); if (head == NULL || head->next==NULL){ return head; } while(head){ ListNode * pNode = head; head = head->next; ListNode *pPrev = &tmpNode; ListNode *pNext = pPrev->next; while (pNext && pNext->val < pNode->val) { pPrev = pNext; pNext = pNext->next; } pPrev->next = pNode; pNode->next = pNext; } return tmpNode.next; } };
相关文章推荐
- leetcode_147题——Insertion Sort List(线性表,插入排序)
- leetcode_c++:链表:Insertion Sort List(147)
- [LeetCode]题解(python):147-Insertion Sort List
- leetcode[147]Insertion Sort List
- leetcode[147]:Insertion Sort List
- leetcode_147 Insertion Sort List
- LeetCode 147 Insertion Sort List
- LeetCode 147 Insertion Sort List
- LeetCode 147 Insertion Sort List
- leetcode-147 Insertion Sort List
- Leetcode 147 Insertion Sort List
- LeetCode147_Insertion Sort List(用插入排序算法对链表进行排序) Java题解
- LeetCode 147 Insertion Sort List
- Java for LeetCode 147 Insertion Sort List
- [leetcode147]insertion sort list
- LeetCode(147) Insertion Sort List
- LeetCode147 Insertion Sort List
- LeetCode(147) Insertion Sort List
- [LeetCode-Algorithms-147] "Insertion Sort List" (2017.12.21-WEEK16)
- [Leetcode] #147 Insertion Sort List