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

[CareerCup] 【第三轮】6.23-6.29 CareerCup 2.4

全局:

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

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

x
2.4 Write code to partition a linked list around a value x, such that all nodes less than x come before all nodes greater than or equal to x.

回复解法可以按照以下格式来
【解题思路】
【时间复杂度】
【空间复杂度】
【gist link】
---------------Optional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】


Notice:
1、记得在程序注释中表明自己算法的时间、空间复杂度
2、代码难懂之处加注释
3、每道题目有对应的帖子,除了贴解法,欢迎讨论,集思广益
4、任何未尽之处,欢迎回报名帖提问,我会进一步作出修改。

上一篇:【第三轮】6.23-6.29 CareerCup 2.3
下一篇:【第三轮】6.23-6.29 CareerCup 2.5
推荐
qianhuang 2014-6-23 20:03:11 | 只看该作者
全局:
【解题思路】
use a pointer p, we guarantee the nodes in [head, p] must be smaller than x. Scan the whole linked list, if the node is smaller than x, insert it  after p, and update p.
【时间复杂度】
O(n)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/qianhuang/adb2f6c2fd73cdc1ff89
回复

使用道具 举报

推荐
sanguine 2014-12-29 08:34:45 | 只看该作者
全局:
Solution3
Idea: modify beyond the original linkedList, just like the partition method in qSort algorithm

Notice: is the implementation below, we just swap the val of the two node, if required to swap the two node:
swap two nodes (node1->next and node2->next) in a linked list,
– first, swap(node1->next, node2->next),
– then, swap(node1->next->next, node2->next->next).

Time Complexity: O(n)
Space Complexity: O(1)

Solution4
Idea: modify beyond the original LinkedList, iterate the list, when the val of the current node is less than x, than move this node to the front of the list

Time Complexity: O(n)
Space Complexity: O(1)

Code:
http://www.jyuan92.com/post-449
回复

使用道具 举报

🔗
chouclee 2014-6-23 14:50:04 | 只看该作者
全局:
【解题思路】创建3个临时的LinkedList lessThan, equal,largerThan,traverse original linkedList,add corresponding elements to 3 LinkedLists respectively.再将equal和largerThan 2个LinkedList append 到lessThan里面,返回 lessThan这个linkedlist
【时间复杂度】O(n)
【空间复杂度】O(n)
【gist link】https://gist.github.com/chouclee/4a1c7b1d06af700a0b44
回复

使用道具 举报

🔗
fang_wu 2014-6-23 19:30:22 | 只看该作者
全局:
【解题思路】创建2个临时的LinkedList lessThan, equalandlargerThan,然后遍历,同时考虑lessthan为null的情况
【时间复杂度】O(n)
【空间复杂度】O(n)
【gist link】https://gist.github.com/qiangusc/14942a249a44fb36de0b
回复

使用道具 举报

🔗
bearkino 2014-6-24 04:02:27 | 只看该作者
全局:
【解题思路】
创建2个临时的LinkedList less, notLess,然后遍历,然后合在一起
【时间复杂度】
O(n)
【空间复杂度】
O(n)
【gist link】
https://gist.github.com/UncleGarden/c5b19fbe945b21a67846
回复

使用道具 举报

🔗
bearkino 2014-6-24 04:02:50 | 只看该作者
全局:
【解题思路】
创建2个临时的LinkedList less, notLess,然后遍历,然后合在一起
【时间复杂度】
O(n)
【空间复杂度】
O(n)
【gist link】
https://gist.github.com/UncleGarden/c5b19fbe945b21a67846
回复

使用道具 举报

🔗
wilbert 2014-6-25 03:50:13 | 只看该作者
全局:
【解题思路】
two temp list headers less and noless, iterate the list and append the node to the corresponding list.
【时间复杂度】
O(N)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/iwilbert/c87a88857239e80785c8
回复

使用道具 举报

🔗
grassgigi 2014-6-25 09:53:50 | 只看该作者
全局:
【解题思路】
Two linked list:
left - all nodes less than x
right - all nodes larger than x

Three pointers track:
Head of left
Tail of left
Head of right

Then normal partition procedure

【时间复杂度】
O(N)

【空间复杂度】
O(1) if allowed to mutate the original list

【gist link】
https://gist.github.com/chrislukkk/3901005117455405cf09
回复

使用道具 举报

🔗
林微熙 2014-6-25 13:48:46 | 只看该作者
全局:
【解题思路】创建2个临时的LinkedList lessThan, equalandlargerThan,然后遍历
【时间复杂度】
O(N)
【空间复杂度】O(n)
【gist link】https://gist.github.com/hilda8519/ddec0a1e20ac8074e79c
回复

使用道具 举报

🔗
habina 2014-6-26 08:25:04 | 只看该作者
全局:
【解题思路】
I have three linkedlist, one for less, one for equal, one for greater, get all value from original linkedlist,
and save it to correspoding linkedlist, lastly connect these three linkedlist.
【时间复杂度】
O(N)
【空间复杂度】
O(N)
【gist link】
https://gist.github.com/habina/719d2ca521e7695311c9
回复

使用道具 举报

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

本版积分规则

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