LintCode 链表插入排序
2017-03-21 22:41
267 查看
题目描述:
用插入排序对链表排序
样例
Given 1->3->2->0->null, return 0->1->2->3->null。
思路分析:
感觉是链表的进阶题。
写完这道题,后面题都很简单啦。
ac代码:
用插入排序对链表排序
样例
Given 1->3->2->0->null, return 0->1->2->3->null。
思路分析:
感觉是链表的进阶题。
写完这道题,后面题都很简单啦。
ac代码:
/** * Definition of ListNode * class ListNode { * public: * int val; * ListNode *next; * ListNode(int val) { * this->val = val; * this->next = NULL; * } * } */ class Solution { public: /** * @param head: The first node of linked list. * @return: The head of linked list. */ ListNode *insertionSortList(ListNode *head) { // write your code here ListNode *r,*dummy; r=new ListNode(0); //dummy=head; //记录原链表的头节点。 while(head!=NULL) { ListNode *node=r; //遍历 r链表。 while(node->next!=NULL && node->next->val<head->val) { node=node->next; } ListNode *temp=head->next; //先保存下来 head 的下一个节点。 head->next=node->next; node->next=head; head=temp; } return r->next; } };
相关文章推荐
- 【lintcode】——链表插入排序-Insertion Sort List
- LintCode(E) 链表插入排序
- 链表插入排序 lintcode
- LintCode 链表插入排序
- Lintcode 173.链表插入排序
- lintcode——链表插入排序
- lintcode-链表插入排序
- 链表插入排序-LintCode
- 链表插入排序-LintCode
- LintCode-链表插入排序
- 【LintCode 简单】173. 链表插入排序
- LintCode-链表插入排序
- lintcode 173 链表插入排序
- [LintCode] 链表插入排序
- LeetCode-147. Insertion Sort List (JAVA)链表插入排序
- 链表排序:冒泡和插入
- C语言实现单向链表及其各种排序(含快排,选择,插入,冒泡)
- LintCode-112.删除排序链表中的重复元素
- lintcode-合并k个排序链表-104
- lintcode ----合并两个排序链表