判断链表有没有环,用Floyd Cycle Detection算法,用两个快慢指针。 判断环的起点,详见 https://leetcode.com/problems/linked-list-cycle-ii/solution/ 中的推导,不过里面应该是 F=(n-1)(a+b)+b,不过对结果没什么 ...
分类:
其他好文 时间:
2018-06-02 19:03:30
阅读次数:
159
问题描述: Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: Example 2: 解题思路: 这道题的rotate的定义很有趣,是把最后一个元素挪到了 ...
分类:
其他好文 时间:
2018-06-01 10:56:56
阅读次数:
172
1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked ...
分类:
其他好文 时间:
2018-05-25 11:08:15
阅读次数:
167
在只能访问待删除节点的情况下删除一个链表的节点,直接把待删除节点的下一个节点值放在待删除节点,然后修改指针,跳过下一个节点即可。 ...
分类:
其他好文 时间:
2018-05-24 18:12:19
阅读次数:
121
判定链表是否为回文 方法一:借助一个栈,将链表节点值全部压入栈中,再弹出,依次比较。本质是把链表节点值逆置,然后与原链表节点值比较。全部相等则为回文。 方法二:先计算链表长度,把前半部分链表逆置,然后比较新链和剩下那段链节点值是否依次相等。 ...
分类:
其他好文 时间:
2018-05-24 16:36:51
阅读次数:
138
新建一个头结点,方便算法实现。和数组删除给定值类似,不删除节点,不改变指针,而是覆盖节点值。通过覆盖节点值来达到压缩链表,实现删除给定值。 一个前指针指向覆盖节点前驱,一个后指针扫描链表。扫到非指定值,则将其赋给前指针的后继,扫到指定值,则仅仅移动后指针,继续扫描直到扫描完整个链表。 扫完之后,将前 ...
分类:
其他好文 时间:
2018-05-22 20:39:24
阅读次数:
137
Description 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 p ...
分类:
其他好文 时间:
2018-05-21 21:16:48
阅读次数:
164
中英题面 在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。 Sort a linked list in O(n log n) time using constant space complexity. 示例 1: 输入: 4->2->1->3 输出: 1->2->3->4 ...
分类:
编程语言 时间:
2018-05-20 10:40:58
阅读次数:
226
question: write a function that takes the first node in a linked list as argument and (destructively) reverses the list, returning the first node in t ...
分类:
其他好文 时间:
2018-05-19 18:36:32
阅读次数:
169
question: write a queue implementation that uses a cicular linked list, which is the same as a linked list except that no links are nul and the value ...
分类:
其他好文 时间:
2018-05-19 17:13:29
阅读次数:
164