简体   繁体   English

如何返回链表的中间节点

[英]How to return the middle node of a linked list

I'm confused about how the while loop condition works in a linked list when checking for the middle node of a linked list 在检查链表的中间节点时,我对链接列表中的while循环条件如何工作感到困惑

This is the correct code that I have for finding the middle node of a linked list 这是我找到链表的中间节点的正确代码

class Node(object):
    def __init__(self, data):
        self.data = data
        self.next = None


class linkedList(object):
    def __init__(self):
        self.head = None

    def append(self, data):
        node = Node(data)
        if self.head == None:
            self.head = node
        temp = self.head
        while temp.next:
            temp = temp.next
        temp.next = node

    def middle(self):
        first = self.head
        second = self.head
        while second and second.next:
            second = second.next.next
            first = first.next
        print(first.data)

If I change the while loop to 如果我将while循环更改为

while second:

or 要么

while second.next:

I receive an error that says 我收到一条错误说

AttributeError: 'NoneType' object has no attribute 'next' on line 24

I'm just wondering why it's important to have both second and second.next 我只是想知道为什么同时拥有secondsecond.next很重要

The solution works by using two pointers. 该解决方案使用两个指针。 First one takes 1 step at a time and second 2 steps at a time. 第一个一步采取一步,一次采用第二步。 However, in taking 2 steps there are two things to verify: 但是,在采取两个步骤时,需要验证两件事:

  1. There is a next step that is valid 有一个有效的下一步
  2. There is a step after the next step mentioned above 在上面提到的下一步之后有一个步骤

If you skip one check it will enter the loop but at the boundary condition it not find the next. 如果你跳过一个检查,它将进入循环但在边界条件下它找不到下一个。 To illustrate: If there are 4 nodes and you only check for second.next at the 3nd node you'll have second.next as valid and you'll enter the while loop but inside that you are directly accessing second.next.next 为了说明:如果有4个节点并且你只在第3个节点检查second.next你将有second.next作为有效,你将进入while循环但在你内部直接访问second.next.next

F,S | F,S | 1 --> 2 --> 3 --> 4 --> None 1 - > 2 - > 3 - > 4 - >无

For starters, your append method doesn't work, and gets stuck in an infinite while loop, since you don't exit out of append on adding the first element. 对于初学者来说,你的append方法不起作用,并且陷入无限循环中,因为在添加第一个元素时你不会退出追加。 The correct version is 正确的版本是

def append(self, data):
    node = Node(data)
    if self.head == None:
        self.head = node
        return
    else:
        temp = self.head
        while temp.next:
            temp = temp.next
        temp.next = node

As to your other question, we want to find the middle of the loop for both even and odd lists, the second.next covers the odd list case, and the second covers the even list case, since either the 2nd pointer will point to null, or it will be null itself, and if you use only one of them, you will get the error you described it, hence you need to have both conditions in the while loop 至于你的另一个问题,我们想找到偶数和奇数列表的循环中间, second second.next覆盖奇数列表的情况, second覆盖偶数列表的情况,因为第二个指针将指向null ,或者它本身就是null,如果你只使用其中一个,你将得到你描述的错误,因此你需要在while循环中同时具备这两个条件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM