题目
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
方法
和有序数组的思想基本一样,将链表进行二分。
TreeNode getBST(ListNode head, int len) {
i...
分类:
其他好文 时间:
2014-06-20 09:46:33
阅读次数:
267
Question:Sort a linked list inO(nlogn) time
using constant space complexity.Solution:Merge sort.找到链表的中间的那个ListNode. 1 /** 2
* Definition for singly-l....
分类:
其他好文 时间:
2014-06-12 17:59:34
阅读次数:
285
问题:对链表进行排序,要求时间复杂度为NlogN。归并排序。
inline ListNode* getMidle(ListNode *head){
if(NULL == head || NULL == head->next)
return head;
ListNode *pslow = head;
ListNode *pfast = head;
while (pfast->next...
分类:
其他好文 时间:
2014-06-10 17:35:56
阅读次数:
282
问题:
给定一个链表的头指针,以及一个整数k,要求将链表按每k个为一组,组内进行链表逆置。少于k个的部分不做处理。
分析:
个人觉得问题的重点是熟悉链表的就地逆置操作,就是头插法。其他的考察点如果还有的话,就的细心程度。
实现:
void reverseList(ListNode *&pre, ListNode *head)
{
ListNode *tail = NULL;
w...
分类:
其他好文 时间:
2014-06-10 17:25:45
阅读次数:
305
Sort a linked list using insertion sort. 1 /**
2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 *
ListNode *nex...
分类:
其他好文 时间:
2014-06-10 12:14:19
阅读次数:
253
问题:
给定个单链表,判断该链表是否存在环。
分析:
这个问题是见的非常多的题目,问题本身而言,技巧性很强,或者说思路很巧妙,这里要说的不是这个题目本身,而是说这种技巧,在很多的地方是用的到的,比如,在寻找单链表的中间节点的时候,就可以用这种形式,一个走两步,一个走一步的形式,来获得中间节点。
//
bool hasCycle(ListNode *head) {
if(...
分类:
其他好文 时间:
2014-06-10 10:48:49
阅读次数:
176
问题:
给定一个有序链表,生成对应的平衡二叉搜索树。
分析
见Convert
Sorted Array to Binary Search Tree
实现:
TreeNode *buildTree(ListNode* head, ListNode *end){
if(head == NULL || head == end)
return NULL;
...
分类:
其他好文 时间:
2014-06-10 07:17:29
阅读次数:
264
Sort a linked list inO(nlogn) time using
constant space complexity./** * Definition for singly-linked list. * struct
ListNode { * int val; * L...
分类:
其他好文 时间:
2014-06-06 20:01:30
阅读次数:
333
Sort a linked list using insertion sort./** *
Definition for singly-linked list. * struct ListNode { * int val; * ListNode
*next; * ListNo...
分类:
其他好文 时间:
2014-06-06 10:39:38
阅读次数:
212
class Solution {public: ListNode
*swapPairs(ListNode *head) { ListNode *a = NULL; ListNode *b = NULL; ListNode
*tail = NULL; ...
分类:
其他好文 时间:
2014-06-05 12:55:24
阅读次数:
216