Java中的LinkedListNode是什么
请原谅我的无知,但我正开始准备我的第一次技术面试,并遇到了有关链表主题的问题和答案
问题:实现一种算法来删除单个链表中间的节点,只允许访问该节点
public static boolean deleteNode(LinkedListNode n) { if (n == null || n.next == null) { return false; // Failure } LinkedListNode next = n.next; n.data = next.data; n.next = next.next; return true; }
我想开始使用此代码(进行更改编译测试),但我不确定如何开始在 Java 中执行此操作。我在 Java 文档中找不到 LinkedListNode 类。
这可能是一个非常愚蠢的问题,但如果有人能指出我正确的方向 - 我会很感激。
编辑
感谢您的快速而有用的回复。我想我的问题不是很清楚。提供上述算法作为该问题的解决方案。我想知道如何在 Java 中实现它,以便我可以使用代码。
谢谢
Excuse my ignorance but I am beginning to prepare for my first technical interview and came across this question and answer on the topic linkedlist
Question: Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node
public static boolean deleteNode(LinkedListNode n) { if (n == null || n.next == null) { return false; // Failure } LinkedListNode next = n.next; n.data = next.data; n.next = next.next; return true; }
I want to start playing with this code (making changes compile test) but I'm not sure how to start doing this in Java. I cannot find the LinkedListNode class in Java docs.
This might be a very silly question but if someone can point me in the right direction - will appreciate it.
EDIT
Thanks for the quick and useful responses. I guess my question was not very clear. The algorithm above was provided as a solution to that question. I wanted to know how to implement that in Java so I can play around with the code.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
仅当列表中有尾节点时,代码才能正常工作。
该算法按照以下逻辑工作:
这是一个使用 LinkedListNode 的类。我应该指出,如果您正在申请程序员职位,您应该能够基本上凭记忆完成此操作。 :-)
The code will only work properly if there's a tail node on the list.
The algorithm works with the following logic
Here's a class that uses LinkedListNode. I should note that if you're applying for a position as a programmer, you should be able to do this basically from memory. :-)
该类很可能是用于此链接列表示例问题的假设类。
This class is most likely a hypothetical class used for this Linked List example question.
LinkedListNode 是一个您将定义来保存数据的类。为了让你上面的例子工作 - 我快速编写了这段代码(只是为了让你理解这个简单的概念),其中我创建了 3 个节点(彼此链接),然后删除中间的一个调用 deleteNode 您在问题中指定的方法。
该代码非常不言自明。让我知道这是否有帮助。
祝你好运
LinkedListNode is a class that you will define to hold data. To get your above example to work - I've quickly written this code (just to make you understand the simple concept) in which I am creating 3 nodes (which are linked to each other) and then deleting the middle one calling the deleteNode method that you have specified in your question.
The code is pretty self explanatory. Let me know if this helps.
Good Luck
这个问题的重要细节与数据结构有关,java只是在这种情况下用于实现的语言。
您应该阅读有关链接列表的维基百科文章,对于这个问题,请注意您的解决方案不'不会产生任何悬空引用或孤立节点。
对这两个粗体术语进行一些搜索,并确保您理解它们
The important details in this question pertain to data structures, java is just the language that is being used to implement in this case.
You should read the wikipedia article about linked lists, and for this question be careful that your solution doesn't produce any dangling references or orphan nodes.
Do some searches on the two terms in bold, and make sure that you understand them
你的问题有点令人困惑。无论您想要一个删除单链表中节点的逻辑,还是想要学习和使用 java LinkedlistNode。
如果您处于第二位,以下链接将为您提供帮助
LinkedListNodee
或者如果你想要逻辑
Your question is bit confusing. whether you want a logic to remove a node in a singly linkedlist or you want to learn and use java LinkedlistNode.
if you are in second the following link will help you
LinkedListNodee
or if you want the logic