链表排序,要求使用 O(nlgn) 时间,常量空间。使用归并的思路/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int ...
分类:
其他好文 时间:
2014-08-16 15:02:00
阅读次数:
162
Given a binary tree, flatten it to a linked list in-place....
分类:
其他好文 时间:
2014-08-16 12:39:00
阅读次数:
136
Convert Sorted List to Binary Search TreeGiven a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST....
分类:
其他好文 时间:
2014-08-16 12:19:20
阅读次数:
227
Problem Description:
Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
You must do this in-place without altering the nodes' values.
For example,
Given...
分类:
其他好文 时间:
2014-08-15 22:34:39
阅读次数:
330
Problem Description:
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1
/ 2 5
/ \ 3 4 6
The flattened tree sho...
分类:
其他好文 时间:
2014-08-15 10:43:28
阅读次数:
198
原题:
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
思路:两条两条地合并。时间复杂度为O(n),n为所有链表节点和。
代码:
/**
* Definition for singly-linked list.
* struct List...
分类:
其他好文 时间:
2014-08-14 20:52:09
阅读次数:
275
这个题目就是用两个指针遍历链表,一个指针每次跳一步,另外一个指针每次跳两步,如果重合,则说明有环。 1 #define NULL 0 2 3 class Solution { 4 public: 5 bool hasCycle(ListNode *head) { 6 Lis...
分类:
其他好文 时间:
2014-08-14 19:27:19
阅读次数:
185
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example, Given 1->2...
分类:
其他好文 时间:
2014-08-14 01:01:07
阅读次数:
230
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the o...
分类:
其他好文 时间:
2014-08-14 00:56:47
阅读次数:
199
Problem Description:
Sort a linked list in O(n log n)
time using constant space complexity.
分析:对链表进行排序,思考排序算法时间复杂度为O(nlogn)的只有归并,快排和堆排序,应用到链表上的归并比较合适,这里利用快慢指针找到链表的中间节点,然后分别对两边递归归并排好序后将两边归并即可得到最终...
分类:
其他好文 时间:
2014-08-13 22:25:57
阅读次数:
234