- 0
- 0
- 约3.89千字
- 约 8页
- 2019-08-14 发布于安徽
- 举报
.
.
Problem 1?: Is it a loop ? (判断链表是否有环?)
Assume that wehave a head pointer to a link-list. Also assumethat we know the list is single-linked. Can you come up an algorithm to checkwhether this link list includes a loop by using O(n) time and O(1) space wheren is the length of the list? Furthermore, can you do so with O(n) time and onlyone register?
方法:使用两个指针,从头开始,一个一次前进一个节点,一个前进2个节点,则最多2N,后两个指针可以重合;如果无环,则正常停止。
同样的,可以找到链表的中间节点。同上。
Problem 2:设计一个复杂度为n的算法找到链表倒数第m个元素。最后一个元素假定是倒数第0个。
提示:双指针查找
Problem 3:用最简单的方法判断一个LONG整形的数A是2^n(2的n次方)
提示:x(x-1)
Problem 4:两个烧杯,一个放糖一个放盐,用勺子舀一勺糖到盐,搅拌均匀,然后舀一勺混合物
原创力文档

文档评论(0)