Leetcode Sort List
来源:互联网 发布:淘宝店铺层级怎么计算 编辑:程序博客网 时间:2024/04/27 05:58
Sort a linked list in O(n log n) time using constant space complexity.
单链表排序问题,下面用插入排序的方法。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:ListNode* sortList(ListNode* head) {ListNode* p = new ListNode(0);p->next = NULL;ListNode *q = p;if (head == NULL)return head;else{p->next = head;head = head->next;p->next->next = NULL;}while (head != NULL){p = q;while (p->next != NULL){if (p->next->val >= head->val){break;}p = p->next;}//insert head point nodeListNode *temp = head;head = head->next;temp->next = p->next;p->next = temp;}return q->next;}};
0 0
- [leetcode][list][sort] Sort List
- Insertion Sort List | leetcode
- Leetcode: Insertion Sort List
- [LeetCode] Insertion Sort List
- Leetcode: Insertion Sort List
- Leetcode: Sort List
- Sort List | leetcode
- leetcode-Sort List
- leetcode-Insertion Sort List
- LeetCode题解:Sort List
- leetcode 134: Sort List
- [LeetCode]Sort List
- [LeetCode]Insertion Sort List
- [LeetCode] Sort List
- 【LeetCode】Insertion Sort List
- leetCode - Sort List
- LeetCode | Insertion Sort List
- LeetCode - Insertion Sort List
- CSS学习笔记---(四)
- Jsp中复选框的全选与反选
- 【JavaMail开发总结】配置文件形式--发送邮件程序
- hdu 1098 Ignatius's puzzle
- 二叉搜索树与双向链表转化
- Leetcode Sort List
- 修炼
- POJ 1556 The Doors 计算几何+MST
- 五招防止QQ密码被盗
- 数据结构学习---队列的链式储存实现
- Repeater和Gridview的区别
- Object类、String类、StringBuffer类、StringBuilder类
- 无法打开包括源文件"extensions/cocos-ext.h"
- Kth Smallest Element in a BST