Sort a linked list using insertion sort.
插入排序:每步将一个待排序的纪录,按其关键码值的大小插入前面已经排序的文件中适当位置上,直到全部插入完为止。
实现代码:
class Solution
{
public:
ListNode *insertionSortList(ListNode *head)
{
if(head=...
分类:
其他好文 时间:
2015-02-11 22:01:01
阅读次数:
167
题意为将小于特定值x的所有节点均放在不小于x的节点的前面,而且,不能改变原来节点的前后位置。
思路:设置两个链表,一个用于存放值小于x的节点,一个用于存放值不小于x的节点。
class Solution {
public:
ListNode *partition(ListNode *head, int x) {
if(head==nullptr)
return head;
...
分类:
其他好文 时间:
2015-02-10 16:47:18
阅读次数:
137
Sort a linked list inO(nlogn) time using constant space complexity./** * Definition for singly-linked list. * struct ListNode { * int val; * L...
分类:
其他好文 时间:
2015-02-09 02:01:37
阅读次数:
212
1 public static ListNode reverseBetween(ListNode head, int m, int n) { 2 ListNode pre=head,current=head,mPre = new ListNode(0),mNode = new L...
分类:
编程语言 时间:
2015-02-06 18:23:59
阅读次数:
141
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *g...
分类:
编程语言 时间:
2015-02-02 21:38:39
阅读次数:
203
原题地址基本模拟代码: 1 ListNode *removeNthFromEnd(ListNode *head, int n) { 2 ListNode *fast = head; 3 ListNode *slow = head; 4 ListNode...
分类:
其他好文 时间:
2015-02-02 15:33:58
阅读次数:
142
原题地址基本链表操作代码: 1 ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { 2 ListNode *h = NULL; 3 ListNode *t = NULL; 4 int carry ...
分类:
其他好文 时间:
2015-01-30 19:13:02
阅读次数:
136
原题地址跟Convert Sorted Array to Binary Search Tree(参见这篇文章)类似,只不过用list就不能随机访问了。代码: 1 TreeNode *buildBST(ListNode *head, int len) { 2 if (len next; 9 ...
分类:
其他好文 时间:
2015-01-30 10:24:52
阅读次数:
192
原题地址链表归并排序真是恶心的一道题啊,哇了好多次才过。代码: 1 void mergeList(ListNode *a, ListNode *b, ListNode *&h, ListNode *&t) { 2 h = t = NULL; 3 while (a && b) { 4 ...
分类:
其他好文 时间:
2015-01-29 20:59:24
阅读次数:
155
原题地址心得:有关链表的题目,多用中间变量,代码写得清晰一点,适当注释代码: 1 ListNode *insertionSortList(ListNode *head) { 2 if (!head) return NULL; 3 4 ListNode...
分类:
其他好文 时间:
2015-01-29 19:09:17
阅读次数:
102