一道超难的算法题-复制链表
Rui在参加面试后,给我分享了一道算法题。他在留言中说made me wanna kill myself when I knew the answer.
题目如下:
Given a Linked List of node structure asstruct Node { type element; Node *next; Node *arb;};You are asked to create an exact copy this linked list. Pointer arb points to an arbitrary node in the linked list.
先说说我的算法吧。我选择了一个超笨的方法完成:创建一个新的linked list存储原始链表每个节点的顺序号,然后复制链表的时候,通过顺序号,找到arb指针所指向的节点。算法很笨,无论是空间效率还是时间效率,都很差。不过好歹是完成了。
不过,在我看完答案之后,我也wanna kill myslef。太神奇了。




