
链表
CAFEBABE丶
颤抖吧丶发际线
展开
-
剑指Offer_链表中倒数第k个节点
题目描述: 输入一个链表,输出该链表中倒数第k个结点。 思路:用两个指针,一个pre,一个last,要求倒数第k个节点,我们可以先让pre先走k步(如果pre还没走到K步就指向null,就说明链表长度不够k,直接返回null),之后,last再走,当pre走到null的时候,last指向的就是倒数第K个节点; 程序: ...原创 2018-06-30 16:56:27 · 163 阅读 · 0 评论 -
LeetCode_rotate-list
题目描述: Given a list, rotate the list to the right by kplaces, where k is non-negative. For example: Given1->2->3->4->5->NULLand k =2, return4->5->1->2->3->NULL. 这个意思有点表...原创 2018-07-21 20:34:05 · 174 阅读 · 0 评论 -
LeetCode_convert-sorted-list-to-binary-search-tree
题目描述: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 思路:二叉查找树的中序遍历就是排序的数,那么我们可以通过中序遍历来构建这个树 直接看代码,下面一般是改进的 ,这里题目要求的是,当节点是偶数的时候,以n/2+1个...原创 2018-07-20 17:33:54 · 164 阅读 · 0 评论 -
LeetCode_copy-list-with-random-pointer
题目描述: 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 of the list. 思路1:用HashMap把原来节点和CoPY节点...原创 2018-07-20 16:27:47 · 206 阅读 · 0 评论 -
LeetCode_linked-cycle-ii
题目描述: Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follow up: Can you solve it without using extra space? 思路(用剑指Offer中HashMap的算法当然也是可以的,不过这题要求不用额外空...原创 2018-07-20 15:03:09 · 134 阅读 · 0 评论 -
LeetCode_reorder-list
题目描述: Given a singly linked list L: L 0→L 1→…→L n-1→L n, reorder it to: L 0→L n →L 1→L n-1→L2→L n-2→… You must do this in-place without altering the nodes' values. For example, Given{1,2,3,4}, re...原创 2018-07-20 14:30:57 · 171 阅读 · 0 评论 -
LeetCode_对链表进行排序
题目描述: Sort a linked list in O(n log n) time using constant space complexity. 思路1:要实现O(nlogn)的话可以考虑使用归并排序,这里有个问题就是寻找中间节点。寻找中间节点的时候,用快慢指针的时候,有两种写法,一种会出现栈溢出,要特别注意。注意事项在代理中...原创 2018-07-20 11:06:06 · 2158 阅读 · 0 评论 -
剑指Offer_两个链表的第一个公共节点
题目描述: 输入两个链表,找出它们的第一个公共结点。 思路1:我们可以这样考虑,先把一个链表遍历一边,然后把所有的节点都存到HashMap的键中,然后遍历另一个链表,每次都和HashMap中的键来对比看看是否存在这个值,如果存在就是第一个公共的节点了。 思路2:注意到这个是单链表,那么第一个公共节点之后所连接的是同一个链! ...原创 2018-07-02 15:59:42 · 252 阅读 · 0 评论 -
剑指Offer_二叉搜索树与双向链表
题目描述: 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。 思路:中序遍历这个二叉树,倒最左边的时候,开始改变树的指针; ...原创 2018-07-02 14:08:57 · 162 阅读 · 0 评论 -
剑指Offer_合并两个排序链表
题目描述: 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。 思路:就是插入排序的思路;程序: ...原创 2018-06-30 17:38:59 · 141 阅读 · 0 评论 -
剑指Offer_反转链表
题目描述: 输入一个链表,反转链表后,输出新链表的表头。 思路:用一个first作为引导节点,first的next一直指向的是反转节点的头节点,要反转一个链表,也就是把这个链表反转这个链表的长度的次数; 程序: Copy: public ListNode ReverseLi...原创 2018-06-30 17:09:40 · 117 阅读 · 0 评论 -
LeetCode_merge-k-sorted-lists
题目描述: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路1:用堆排序,每次都是从k条链表里面取出这k个节点中的最小值,然后拼接起来,取这个k个节点就可以使用堆来操作了; 思路2:用分治的思想,和归并排序一样,每次合...原创 2018-07-21 22:08:41 · 158 阅读 · 0 评论