【题目】
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
【解析】
分治:用快慢指针找到链表的中点,作为树的root,然后二分——中点之前的链表和中点之后的链表分别再构造二叉平衡树。
/**
* Defin...
分类:
其他好文 时间:
2014-11-24 22:39:00
阅读次数:
218
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.
Your algorithm should use only constant space. Y...
分类:
编程语言 时间:
2014-11-24 10:10:53
阅读次数:
155
Sort a linked list in O(n log n)
time using constant space complexity.
大家看完题目估计跟我一样啦。。。都在想哪些是nlogn啊~快速排序、归并排序、堆排序!然后开始愁,这些东西变成list了可怎么办啊。。。
可是我深深地记得在CMU的时候老师告诉我,java现在自带的Arrays.sort用的是快排,然后我就想,那么…...
分类:
编程语言 时间:
2014-11-24 10:10:04
阅读次数:
170
Given a linked list, reverse the nodes of a linked listkat a time and return its modified list.If the number of nodes is not a multiple ofkthen left-o...
分类:
其他好文 时间:
2014-11-23 23:08:59
阅读次数:
332
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy ...
分类:
其他好文 时间:
2014-11-23 23:01:16
阅读次数:
181
原题地址:https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/题目内容:Given a singly linked list where elements are sorted in ascending...
分类:
其他好文 时间:
2014-11-23 21:34:38
阅读次数:
141
【题目】
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1
/ 2 5
/ \ 3 4 6
The flattened tree should look like:...
分类:
其他好文 时间:
2014-11-23 17:35:12
阅读次数:
131
这题和Reverse Node in k-Group相关,主要是看如何翻转一个链表。这里是指定区间从第m个到第n个的翻转例如:Given1->2->3->4->5->NULL,m= 2 andn= 4,return1->4->3->2->5->NULL./** * Definition for si...
分类:
其他好文 时间:
2014-11-23 17:22:49
阅读次数:
172
void Insert(LinkedList * &start, int val){ LinkedList * cur = start; if (start == NULL) { auto l = new LinkedList(val); l->pre ...
分类:
其他好文 时间:
2014-11-23 17:22:34
阅读次数:
194
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.C++代码实现:#include#include#include#includeu...
分类:
其他好文 时间:
2014-11-23 15:53:30
阅读次数:
256