LeetCode----Insertion sort list
2014-02-16 17:11
302 查看
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* insertionSortList(ListNode *list) { ListNode *new_list(NULL), *next(NULL); while(list) { next = list->next; new_list = InsertNode(new_list, list); list = next; } return new_list; } private: ListNode* InsertNode(ListNode *list, ListNode *node) { //insert node into a sorted list ListNode *pre(NULL), *cur(list); while(cur && cur->val < node->val) { pre = cur; cur = cur->next; } if(pre == NULL){ node->next = cur; return node; } else{ node->next = cur; pre->next = node; } return list; } };
相关文章推荐
- Leetcode: Insertion Sort List
- LeetCode-Insertion Sort List
- Leetcode: insertion-sort-list
- Insertion Sort List--LeetCode
- LeetCode Insertion Sort List
- leetcode - Insertion Sort List
- Insertion Sort List - Leetcode
- 【LeetCode】-Insertion Sort List
- leetcode做题总结,题目Insertion Sort List 2013/11/12
- leetcode_Insertion Sort List
- LeetCode(147) Insertion Sort List
- leetcode---insertion-sort-list---链表
- leetcode---Insertion Sort List
- LeetCode: Insertion Sort List
- Insertion Sort List ---LeetCode
- LeetCode---Insertion Sort List
- LeetCode – Refresh – Insertion Sort List
- LeetCode Insertion Sort List 最新题解
- LeetCode Insertion Sort List
- leetcode - Insertion Sort List