确定链表中的第一个节点
如果链表的最后一个节点连接到第一个节点,则形成一个环。 那么如何识别链表中哪个节点是第一个节点和最后一个节点呢?
If the last node of a linked list is connected to the first node, it makes a ring.
Then how would you identify which of the nodes in the linked list is the first node and the last one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你不会。如果是环,那么第一个和最后一个就没有意义了。任何节点都可以是第一个或最后一个。
如果您将“第一个”定义为“首先创建”,那么您可能需要向节点添加一些排序信息以便能够知道这一点。
You wouldn't. If it is a ring, then first and last are meaningless. Any node can be first or last.
If you define "first" as "created first", then you would probably want to add some sequencing information to the nodes to be able to know that.
大概会有一个指向链表第一个节点的指针(您需要一种进入列表的方法)。此外,在这种情况下,维护指向列表中最后一个节点的指针也很方便。
如果您更具体地了解您需要了解的内容,我可以为您提供更多帮助。
Presumably there would be a pointer to the first node of a linked list (you need a way of entering the list). Additionally it is convenient in this case to maintain a pointer to the last node in the list.
If you are more specific about what you need to know I can be more helpful.
你所描述的是一个循环链表。仅通过维护最后一个节点就可以知道列表中的第一项和最后一项。从逻辑上讲,这要求它的后继节点成为第一个节点。
维基百科有更多关于它的信息: https://en.wikipedia.org/wiki/Linked_list# Circularly_linked_list
What you are describing is a circularly linked list. It is possible to know both the first and last item in the list based off just maintaining the last node. Logically that requires it's successor to be the first node.
wikipedia has a bit more about it: https://en.wikipedia.org/wiki/Linked_list#Circularly_linked_list
如果您尝试实现循环列表,请查看此处 http://en.wikipedia.org /wiki/Linked_list#Circularly_linked_list
If you are trying to implement a circular list, have a look here http://en.wikipedia.org/wiki/Linked_list#Circularly_linked_list
创建链表时,将第一个节点的地址存储在一个变量(最好是私有变量)中,以便在任何时候,您都可以将该地址与当前节点的地址进行比较
When you create the Linkedlist, store the address of the first node in a variable(preferably a private variable) so that at any point of time , you can compare this address with the current node's address