Python 错误返回链接列表中位置“m”处节点的值

Python 错误返回链接列表中位置“m”处节点的值,python,algorithm,nodes,Python,Algorithm,Nodes,我有一个家庭作业问题: 在一个单链接列表中查找元素,该列表从末尾起有m个元素。 例如,如果链表有5个元素,则从末尾算起的第3个元素就是第3个元素。函数定义应该类似于question5ll,m,其中ll是链表的第一个节点,m是从末尾算起的第m个编号。您应该复制/粘贴下面的节点类,以用作链接列表中节点的表示形式。返回该位置节点的值 要粘贴的代码是类节点的4行定义。我的尝试随之而来 class Node(object): def __init__(self, data): self.dat

我有一个家庭作业问题:

在一个单链接列表中查找元素,该列表从末尾起有m个元素。 例如,如果链表有5个元素,则从末尾算起的第3个元素就是第3个元素。函数定义应该类似于question5ll,m,其中ll是链表的第一个节点,m是从末尾算起的第m个编号。您应该复制/粘贴下面的节点类,以用作链接列表中节点的表示形式。返回该位置节点的值

要粘贴的代码是类节点的4行定义。我的尝试随之而来

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

ll = None

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

# Function for adding values to list.
def add(new_data):
    global ll
    node = Node(new_data)
    node.next = ll
    ll = node

def Question5(ll, m):
    # Start of list.
    element = ll
    # Node count.
    c = 0
    # Set end of list to False.
    endlist = False
    # Loop until the linked list reaches its end.
    while not endlist:
        c += 1
        element = element.next
        if element.next == None:
            endlist = True

    # Set node value as count - position m
    position = c - m
    # If position is less than 0, end has been reached
    if(m < 0):
        return "8, End of list"
    # If not the end of list return value at position m
    else:
        return position

global ll
add(1)
add(2)
add(3)
add(4)
add(5)
add(6)
add(7)
add(8)
print ("Value of node at position m from the end is: ", Question5(ll, -1))
#('Value of node at position m from the end is: ', '8, End of list')
print ("Value of node at position m from the end is: ", Question5(ll, 3))
#('Value of node at position m from the end is: ', 4)
print ("Value of node at position m from the end is: ", Question5(ll, 2))
#('Value of node at position m from the end is: ', 5)
print ("Value of node at position m from the end is: ", Question5(ll, 0))
#('Value of node at position m from the end is: ', 7)
我的审阅者说我的解决方案不正确,因为我没有返回位置“m”处节点的实际值。在这种情况下,返回节点对象值的正确方法是什么

编辑:为了最后确定这个问题,我在下面的第二个答案中加入了我能够按照建议实施的解决方案

新代码:

ll = None

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

# Function for adding values to list.
def add(new_data):
    global ll
    node = Node(new_data)
    node.next = ll
    ll = node

def Question5(ll, m):
    # Nodes already traversed
    element1 = ll
    # Nodes from beginning
    element2 = ll
    # Node count.
    c = 0

    # Traverse list until c = m
    if(ll is not None):
        while(c < m):
            element2 = element2.next
            c += 1

    # Loop until the linked list reaches its end.
    while(element2 is not None):
        #print ("This is element 1:", element1.data)
        #print ("This is element 2:", element2.data)
        element1 = element1.next
        element2 = element2.next

    # return the current node at m steps from the end
    return element1.data

global ll
# List meant to represent a telephone with 0 being the end.
add("0")
add("9 WXYZ")
add("8 TUV")
add("7 PQRS")
add("6 MNO")
add("5 JKL")
add("4 GHI")
add("3 DEF")
add("2 ABC")
add("1")
# Singly Linked List:
# 1 - 2 ABC - 3 DEF - 4 GHI - 5 JKL - 6 MNO - 7 PQRS - 8 TUV - 9 WXYZ - 0
print ("node at position m in relation to the end of ll is: ", Question5(ll, 4))
# ('node at position m in relation to the end of ll is: ', '7 PQRS')
print ("node at position m in relation to the end of ll is: ", Question5(ll, 8))
# ('node at position m in relation to the end of ll is: ', '3 DEF')
print ("node at position m in relation to the end of ll is: ", Question5(ll, 1))
# ('node at position m in relation to the end of ll is: ', '0')
print """---End Question 5---
看看您的输出:当m为2时,代码返回5。问题陈述清楚地表明它期望7,第二个元素计数形成列表的末尾。您的代码回退了两个额外的元素:显然,允许返回8的唯一方法是给出一个-1的位置,而0需要得到一个7的值

把你的计数搞定,你就没事了;您已经正确地处理了遍历

现在,我已经有时间查看代码了,我将稍微偏离我以前的回答:您确实正确地到达了列表的末尾,但这就是您的所有代码都正确地完成的部分。从功能的角度来看,您的审阅者是完全正确的:您根本不返回节点的值。计算位置并假设节点的值匹配。你不允许那样做

考虑一个这样构建的列表,在电话按钮上:

add("ABC")
add("DEF")
add("GHI")
add("JKL")
add("MNO")
add("PRS")
add("TUV")
add("WXY")
add("QZ")
如果输入为2,则必须返回WXY。我刚刚用你的Question5代码试过,它仍然返回数字。错

您必须找到到达列表末尾的方法,然后从列表末尾找到合适的值。有多种方法可以做到这一点。例如,在遍历列表时构建节点值列表;然后只需打印列表中位置-m的值。另一种可能是使用递归来查找列表的结尾,然后返回一个计数器来备份递归链,以确定哪个实例应该打印当前节点的值


你能自己采取下一步吗。

这意味着我应该先使用c=1,而不是c=0?我被标记为这一点的方式使我相信我做了错误的遍历,因为这是我的审阅者告诉我需要做的。你是说我已经正确地进行了遍历。。我应该和我的评论者讨论这个问题吗?是的,我想我可以从这里继续,谢谢你的深入反馈。请注意你的代码仍然是错误的。例如,在第一个测试用例中,当您应该从末端返回第四个节点(即4 GHI)时,您应该从前端返回第四个节点,即7 PQR。