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

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

全局:

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

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

x
Implement an algorithm to find the kth to last element of a singly linked list.

发帖规范:
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.1
下一篇:[第二轮] 2/25-3/3 CareerCup 2.3
🔗
zuohr 2013-2-24 15:36:00 | 只看该作者
全局:
本帖最后由 zuohr 于 2013-2-24 15:37 编辑

Use two "pointers" iterate through the linked list, the second one lags behind the first one by k position.
Runtime complexity O(N), storage complexity O(1).
https://gist.github.com/Zuohr/5023018
回复

使用道具 举报

🔗
starcroce 2013-2-24 17:53:04 | 只看该作者
全局:
https://github.com/starcroce/car ... r/careercup_2_2.cpp
本来想法很直接,先遍历一遍拿到长度再去找倒数第k个,然后看了一楼的算法之后重新改了下。。。
先看k和head合不合法,然后就设两个相距k的指针往后移,一个到底了另一个也就到倒数k个了。。。
回复

使用道具 举报

🔗
jimwallet 2013-2-24 22:02:35 | 只看该作者
全局:
https://gist.github.com/njcongtou/4999347

1。 use two pointers, k interval between them.
回复

使用道具 举报

🔗
mazl123321 2013-2-24 23:12:15 | 只看该作者
全局:
回复

使用道具 举报

🔗
grassgigi 2013-2-25 01:17:55 | 只看该作者
全局:
刚开始看成kth largest element to the end..
kth element的话,一样是两个pointer,tracking down to the end of the list
https://gist.github.com/chrislukkk/5023048/edit
回复

使用道具 举报

🔗
grassgigi 2013-2-25 01:18:33 | 只看该作者
全局:
刚开始看成kth largest element to the end..
kth element的话,一样是两个pointer,tracking down to the end of the list
https://gist.github.com/chrislukkk/5023048/edit
回复

使用道具 举报

🔗
mapleman 2013-2-25 02:10:07 | 只看该作者
全局:
这道题二楼所说的那种做法,先遍历一遍拿到长度再去找倒数第k个,比答案差在哪里呢?
谢谢!

评分

参与人数 2大米 +4 收起 理由
TonyAnn + 3 给你点个赞!
XuYan2019 + 1 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
starcroce 2013-2-25 04:04:30 | 只看该作者
全局:
mapleman 发表于 2013-2-25 02:10
这道题二楼所说的那种做法,先遍历一遍拿到长度再去找倒数第k个,比答案差在哪里呢?
谢谢!

时间复杂度都是O(n),但是改进后的做法只需要遍历一遍,我的最初想法相当于遍历两遍,所以改进后常数项上做了点优化。。。
回复

使用道具 举报

🔗
liuzhe1218 2013-2-25 16:26:09 | 只看该作者
全局:
https://gist.github.com/mettaworld/5028492
java language
find the kth node with a loop, then output the nodes from kth to the last with a loop
回复

使用道具 举报

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

本版积分规则

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