Loop in single linked list

來源: 外-國人 2009-05-15 07:55:51 [] [舊帖] [給我悄悄話] 本文已被閱讀: 次 (316 bytes)
本文內容已被 [ 外-國人 ] 在 2009-06-09 09:31:02 編輯過。如有問題,請報告版主或論壇管理刪除.
Wow, 幾天不見,這裏正熱鬧,看來還是Progromers比Mathematician多。怪不得康MM被氣得離家出走了。:)
既然大家對Programing感興趣,給大家做一個。不過,哪位想Progrom也可以。
Any language is ok.

(An interview question)
Given a single linked list with n items.
Find an O(n) algorithm to detect any loops.

所有跟帖: 

這題很經典 -endofsuburbia- 給 endofsuburbia 發送悄悄話 endofsuburbia 的博客首頁 (32 bytes) () 05/15/2009 postreply 09:26:24

2X追就可了。。。 -haha2000- 給 haha2000 發送悄悄話 (0 bytes) () 05/15/2009 postreply 12:51:13

沒錯。設置兩個pointers,一個追另一個。追上為有Loop。 -戲雨飛鷹- 給 戲雨飛鷹 發送悄悄話 戲雨飛鷹 的博客首頁 (0 bytes) () 05/15/2009 postreply 14:46:34

回複:沒錯。設置兩個pointers,一個追另一個。追上為有Loop。 -外-國人- 給 外-國人 發送悄悄話 (16 bytes) () 05/17/2009 postreply 11:03:27

a similar but harder problem -dynamic- 給 dynamic 發送悄悄話 (122 bytes) () 05/18/2009 postreply 09:43:31

sum mod n -haha2000- 給 haha2000 發送悄悄話 (0 bytes) () 05/18/2009 postreply 09:52:57

I asked a similar question in an interview. -亂彈- 給 亂彈 發送悄悄話 亂彈 的博客首頁 (162 bytes) () 05/18/2009 postreply 10:01:54

you are definitely right... I need to fix it :) -dynamic- 給 dynamic 發送悄悄話 (0 bytes) () 05/18/2009 postreply 10:48:03

fix of the problem -dynamic- 給 dynamic 發送悄悄話 (160 bytes) () 05/18/2009 postreply 10:51:26

algorithmic complexity requirement? -haha2000- 給 haha2000 發送悄悄話 (103 bytes) () 05/18/2009 postreply 19:19:21

can be done in linear time... -haha2000- 給 haha2000 發送悄悄話 (150 bytes) () 05/18/2009 postreply 19:42:41

it takes a bit more than that -dynamic- 給 dynamic 發送悄悄話 (438 bytes) () 05/18/2009 postreply 19:55:29

hmm,這個有意思:) -戲雨飛鷹- 給 戲雨飛鷹 發送悄悄話 戲雨飛鷹 的博客首頁 (0 bytes) () 05/18/2009 postreply 21:12:19

回複:it takes a bit more than that -utopian- 給 utopian 發送悄悄話 (51 bytes) () 05/20/2009 postreply 17:23:11

hashing needs linear memory as well. we want constant memory her -dynamic- 給 dynamic 發送悄悄話 (0 bytes) () 05/20/2009 postreply 17:28:12

Pay attention to some key numbers. -亂彈- 給 亂彈 發送悄悄話 亂彈 的博客首頁 (0 bytes) () 05/20/2009 postreply 19:24:24

覺得在哪裏見過這個題目 -haha2000- 給 haha2000 發送悄悄話 (19 bytes) () 05/21/2009 postreply 07:24:10

I don't think so. -亂彈- 給 亂彈 發送悄悄話 亂彈 的博客首頁 (0 bytes) () 05/21/2009 postreply 09:52:29

這題目是IMB 2004 一月的一個puzzle:) -戲雨飛鷹- 給 戲雨飛鷹 發送悄悄話 戲雨飛鷹 的博客首頁 (79 bytes) () 05/21/2009 postreply 12:22:12

Yes, right! thanks! -haha2000- 給 haha2000 發送悄悄話 (0 bytes) () 05/22/2009 postreply 11:05:23

solution -dynamic- 給 dynamic 發送悄悄話 (572 bytes) () 05/20/2009 postreply 21:41:18

hmmm,佩服. "形狀像一個勺子", haha... -戲雨飛鷹- 給 戲雨飛鷹 發送悄悄話 戲雨飛鷹 的博客首頁 (0 bytes) () 05/21/2009 postreply 12:25:03

這個題目曾是微軟的麵試題目。但是沒有了數組'read only'的條件, -戲雨飛鷹- 給 戲雨飛鷹 發送悄悄話 戲雨飛鷹 的博客首頁 (75 bytes) () 05/21/2009 postreply 13:12:13

加跟帖:

當前帖子已經過期歸檔,不能加跟帖!