栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在恒定时间内将节点插入链表?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在恒定时间内将节点插入链表?

如果遇到问题,只需写下来即可:

// First we have a pointer to a node containing element (elm) // with possible a next element.// Graphically drawn as:// p -> [elm] -> ???tmp = new Node();// A new node is created. Variable tmp points to the new node which // currently has no value.// p   -> [elm] -> ???// tmp -> [?]tmp.element = p.element;// The new node now has the same element as the original.// p   -> [elm] -> ???// tmp -> [elm]tmp.next = p.next;// The new node now has the same next node as the original.// p   -> [elm] -> ???// tmp -> [elm] -> ???p.element = y;// The original node now contains the element y.// p   -> [y] -> ???// tmp -> [elm] -> ???p.next = tmp;// The new node is now the next node from the following.// p   -> [y] -> [elm] -> ???// tmp -> [elm] -> ???

您已达到要求的效果,但是它可以提高效率,我敢打赌,您现在就可以了解自己。

写这样的东西更清晰:

tmp = new Node();tmp.element = y;tmp.next = p;p = tmp;

如果p不可变,那当然不起作用。但是,如果p == NULL,则您的算法将失败。

但是我要说的是,如果您对算法有疑问,只需写下效果即可。尤其是对于树和链表,您需要确保所有指针都指向严格的方向,否则会造成混乱。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/570993.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号