查看: 5170| 回复: 39
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] [第二轮] 2/25-3/3 CareerCup 2.6

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
Given a circular linked list, implement an algorithm which returns the node at the beginning of the loop.
DEFINITION
Circular linked list: A (corrupt) linked list in which a node's next pointer points to an earlier node, so as to make a loop in the linked list.
EXAMPLE
Input: A -> B -> C -> D -> E -> C [the same C as earlier]
Output: C

发帖规范:
http://www.1point3acres.com/bbs/thread-48094-1-1.html
http://www.1point3acres.com/bbs/thread-32423-1-1.html

评分

参与人数 1大米 +10 收起 理由
Kimurate + 10

查看全部评分


上一篇:[第二轮] 2/25-3/3 CareerCup 2.5
下一篇:有人做过leetcode里的word ladder II 吗?
推荐
daisyang 2014-8-16 14:30:48 | 只看该作者
全局:
两个指针一快一慢,经典算法吧
时间复杂: O(N)
空间复杂:O(1)

https://gist.github.com/daisyang/5521dd686dcb792ff86d
回复

使用道具 举报

🔗
EchoMemory 2013-2-24 13:51:20 | 只看该作者
全局:
store each appearing pointer, compare the pointer to the pool while iterate the node O(N)
Btw, what does the ‘corrupt’ mean...
回复

使用道具 举报

🔗
lvchaoshuai 2013-2-24 18:46:50 | 只看该作者
全局:
EchoMemory 发表于 2013-2-24 13:51
store each appearing pointer, compare the pointer to the pool while iterate the node O(N)
Btw, what ...

Maybe it means the order of the list is corrupt.
回复

使用道具 举报

🔗
EchoMemory 2013-2-24 20:50:04 | 只看该作者
全局:
lvchaoshuai 发表于 2013-2-24 18:46
Maybe it means the order of the list is corrupt.

I see... the word is a bit strange...
回复

使用道具 举报

🔗
sing1ee 2013-2-26 15:11:14 | 只看该作者
全局:
I use two pointers.
The first pointer move front with one step, and the second move front with two steps.
when they meet each other, set the second pointer to the start of the linked list.
Move one step at the same time, when they meet again, the node is the start of the circle.

gist: https://gist.github.com/sing1ee/5036554
回复

使用道具 举报

🔗
liuzhe1218 2013-2-26 15:14:05 | 只看该作者
全局:
lz, can I comprehand in this way: the stored address in the second C is A. So I should write a code to find the second C?

点评

I think the second C is the same node as the first C and the address stored in C is D.  发表于 2013-3-2 03:44
回复

使用道具 举报

🔗
grassgigi 2013-2-26 15:59:21 | 只看该作者
全局:
本帖最后由 grassgigi 于 2013-2-27 01:11 编辑

one node pointer forwards two step each time, called fast node
one node pointer forwards one step each time, called slow node
Since two nodes will in loop, so they will meet in somewhere sometime
最后还是参考了答案, 实现了一下答案,确实精妙..
https://gist.github.com/chrislukkk/5040122
回复

使用道具 举报

🔗
grassgigi 2013-2-26 16:01:07 | 只看该作者
全局:
liuzhe1218 发表于 2013-2-26 15:14
lz, can I comprehand in this way: the stored address in the second C is A. So I should write a code  ...

I think the second C is the same node as the first C so the loop is C->D->E->C, so node C is the start point of the loop in this link list, which should return as required
回复

使用道具 举报

🔗
sing1ee 2013-2-27 14:12:02 | 只看该作者
全局:
grassgigi 发表于 2013-2-26 15:59
one node pointer forwards two step each time, called fast node
one node pointer forwards one step e ...

代码写得比我认真
回复

使用道具 举报

🔗
champoier 2013-2-28 14:25:48 | 只看该作者
全局:
two pointers, 一个步长为1,一个步长为2,从头开始扫,第一次聚合后,一个指针只回头,另一个从当前位置起,两个一起一步长为1移动……再次聚首时,则为要找的点。

https://gist.github.com/skxie/674da5d07d7387ad6e11
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表