需要帮助修复链接列表实现+追加方法

时间:2022-02-04 21:02:23

For practice and to prepare for programming interviews I've by doing problem sets from cracking the interview by Gayle McDowell. That being said I've been answering all the problems in both Python and Java. I'm working with linked list in python at the moment, since i've completed it all in Java already. I'm having trouble adding cargo/node data to the end/tail of my linked list. This was not a problem in Java and I suppose my first mistake is that I'm trying to implement this method the same way I did in Java.

为了练习和准备编程采访,我通过解决Gayle McDowell的采访来解决问题。话虽如此,我一直在回答Python和Java中的所有问题。我现在正在使用python中的链表,因为我已经用Java完成了所有这些。我在将货物/节点数据添加到链表的末尾/尾部时遇到了麻烦。这在Java中不是问题,我想我的第一个错误是我正在尝试以与在Java中相同的方式实现此方法。

The is the method I need help with

这是我需要帮助的方法

  def append(self, data):.
        new_node = Node()
        new_node = data
        node = self.cur_node
        while node:
            node = node.next
        node.next = new_node

The problem i'm having is that node.next is set to None after the while loop. I thought that if I iterated over the linked list and added my data the to tail end this is how I would go about it. I assumptions are largely based on how I implemented it in Java which I added below just for reference.

我遇到的问题是while循环后node.next设置为None。我认为,如果我遍历链表并将我的数据添加到尾端,这就是我要去做的事情。我的假设很大程度上取决于我是如何用Java实现的,我在下面添加它仅供参考。

 void appendToTail(int d){

        Node end = new Node(d); //Item to append to the end
        Node n = this; //To access Class object next

        while(n.next != null){
            n = n.next;
        }
        n.next = end;
    }

The rest of my python class looks like this, just incase you need to see how the rest of the link list is implemented.

我的python类的其余部分看起来像这样,只需要看看链接列表的其余部分是如何实现的。

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

class LinkedList(object):
    def __init__(self):
        self.cur_node = None

    def add(self, data):
        new_node = Node()
        new_node.data = data
        new_node.next = self.cur_node
        self.cur_node = new_node

    #write a method that appends to the tail instead of the head
    def append(self, data):.
        new_node = Node()
        new_node.data = data
        node = self.cur_node
        while node:
            node = node.next
        node.next = new_node #This is the problem line

    def list_print(self):
        node = self.cur_node # cant point to ll!
        while node:
            print node.data, 
            node = node.next

    # answer to question 2_1
    # Write code to remove duplicates from an unsorted linked list
    def remove_dup(self):
        lset = set()
        previous = Node()
        node = self.cur_node
        while node:
            if node.data in lset:
                previous.next = node.next
            else:
                lset.add(node.data)
                previous = node
            node = node.next

I would love some direction on how to go about fixing or changing my append method.

我想对如何修复或改变我的追加方法有一些指导。

1 个解决方案

#1


Ok, this is what the function should look like:

好的,这就是函数应该是这样的:

def append(self, data):
    new_node = Node()
    new_node.data = data
    node = self.cur_node
    while node.next:
        node = node.next
    node.next = new_node #This is the problem line

You were iterating past the end of the list with your original while condition. Instead you want to stop on the last node, which is the one with next == None. and also you apparently had a typo where you were setting the node itself to the data, instead of its data attribute.

您使用原始条件迭代超出列表末尾。相反,您希望停在最后一个节点上,即下一个节点==无。而且你显然有一个拼写错误,你将节点本身设置为数据,而不是其数据属性。

#1


Ok, this is what the function should look like:

好的,这就是函数应该是这样的:

def append(self, data):
    new_node = Node()
    new_node.data = data
    node = self.cur_node
    while node.next:
        node = node.next
    node.next = new_node #This is the problem line

You were iterating past the end of the list with your original while condition. Instead you want to stop on the last node, which is the one with next == None. and also you apparently had a typo where you were setting the node itself to the data, instead of its data attribute.

您使用原始条件迭代超出列表末尾。相反,您希望停在最后一个节点上,即下一个节点==无。而且你显然有一个拼写错误,你将节点本身设置为数据,而不是其数据属性。