多少人可以在一小时内做出来,面试题插入排序单链表

2019-06-10 20:16:05 +08:00
 greenlake
让你用你熟悉的语言写一个插入排序单链表的算法题,你可以在一小时内写出来吗,并配上单元测试,同时编译通过
6137 次点击
所在节点    程序员
44 条回复
greenlake
2019-06-10 21:57:32 +08:00
单元测试 1:
Input: 4->2->1->3
Output: 1->2->3->4

单元测试 2:
Input: -1->5->3->4->0
Output: -1->0->3->4->5
tt67wq
2019-06-10 22:32:21 +08:00
有木有空间复杂度要求啊?我 values 抠出来,排好序再拼成一个链表行不行啊?
whileFalse
2019-06-10 23:00:27 +08:00
俺连插入排序是啥都不知道了
jc89898
2019-06-10 23:08:25 +08:00
10 分钟 算法 101 学的吧
AlexLixin
2019-06-10 23:29:15 +08:00
package demo;

class Node {
public Node next;
public int data;

public Node() {
}

public Node(Node next, int data) {
this.next = next;
this.data = data;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Node p = this;
while(p!=null) {
sb.append(p.data+"->");
p=p.next;
}
String string = sb.toString();
return string.substring(0,string.length()-2);
}

}

public class Demo {
public static void main(String[] args) {
test1();
test2();
}

private static void test2() {
Node head = new Node(new Node(new Node(new Node(null, 3), 1), 2), 4);
Node sorted = sort(head);
System.out.println(sorted);
}

private static void test1() {
Node head = new Node(new Node(new Node(new Node(new Node(null,0), 4), 3), 5), -1);
Node sorted = sort(head);
System.out.println(sorted);
}

private static Node sort(Node head) {
Node sortedL, sortedR;
sortedL = head;
sortedR = head;
Node current = head;
while (sortedR.next != null) {
current = sortedR.next;
if (current.data < sortedL.data) {
sortedR.next = current.next;
current.next = sortedL;
sortedL = current;
} else if (sortedL.data <= current.data && current.data < sortedR.data) {
Node p = sortedL;
while (p != sortedR) {
if (p.data <= current.data && current.data < p.next.data) {
sortedR.next = current.next;
current.next = p.next;
p.next = current;
break;
}
p = p.next;
}
} else {
sortedR = current;
}

}
return sortedL;
}
}
ArthurRen
2019-06-10 23:35:54 +08:00
这种题目需要一个小时吗。。。。
leetcode easy 难度吧
xuanbg
2019-06-10 23:44:29 +08:00
话说有谁真的写过链表的实现?
ayyll
2019-06-10 23:51:59 +08:00
@xuanbg 这。。。acm 集训队初级成员也要写的吧。。初期应该是禁 stl 的 所以遇到链表的题必须要手写啊
hx1997
2019-06-11 01:19:12 +08:00
@xuanbg ?大学数据结构课不用写吗🤦
greenlake
2019-06-11 01:29:57 +08:00
@ArthurRen 当然大学刚毕业的,或者准备面试的应该可以。但是我想讨论那些在工作岗位上做了几年,但是平时很少用到这样的算法,如果直接让你写,有多少能写出来,天天刷 leetcode 的除外
greenlake
2019-06-11 01:30:31 +08:00
@tt67wq 应该是 in place 的排序
greenlake
2019-06-11 01:32:19 +08:00
@AlexLixin 请不要 copy & paste
zhy0216
2019-06-11 01:45:42 +08:00
闭着眼睛写 认识到自己的不足才能进步 不要奢望每个人都这么弱。
smdbh
2019-06-11 01:59:21 +08:00
知易行难
tyrealgray
2019-06-11 02:09:04 +08:00
如果算上搭单元测试的环境而且还用的 C++的话或许会接近一个小时吧。但是这个算法,即使你半路出家一开始没学过数据结构,工作几年还写不出来的话不应该吧。
jc89898
2019-06-11 05:16:18 +08:00
@xuanbg 链表的实现不是大一基础课教的吗...
exonuclease
2019-06-11 05:34:33 +08:00
链表的插入排序挺好写的啊 我记得 leetcode 做出来没超过 20 分钟
exonuclease
2019-06-11 05:36:08 +08:00
@exonuclease 记错了 是归并排序
AlexLixin
2019-06-11 08:24:16 +08:00
@greenlake 我三十多分钟写出来的。比想象中花的时间多,平常不怎么用到的话,细节上不可能不出错。
pwrliang
2019-06-11 08:24:59 +08:00
我觉得给我半个点能写出来…我中午午休试试

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/572589

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX