繁体   English   中英

我是否在链接列表中使用正确的 append function,使用 python?

[英]Am i using right append function in Linked list, using python?

我通过做作业来学习链表,制作链表的代码运行良好。 但是,操纵它不能正常工作。 所以,我有一个带头的链接列表,我创建了一个 function ,它需要两个 arguments (头,你想在第一个 append 的最后几个数字)。 function 的目的是,假设你有一个链表。 1 -> 4->5->6->2->3->9 我希望最后三个数字出现在第一个数字上,剩下的前四个数字出现在这三个数字的最后一个数字上,就像这样。 2->3->9->1 -> 4->5->6 所以,我需要断开 2 与其前一个数字(即 6)之间的链接,并将 6.next 链接到 None 和然后将 9.next 连接到 1,以获得所需的结果。 但是,我在运行无限时间的代码中遇到了一些问题,而且有点错误。 你能看看这个并帮助我吗? 谢谢

class Node: # Class will intantiate a node object def __init__(self, data): self.data = data # node have a data self.next = None # node have a link as well ( of the next node), which is None currently.

 def typeinput(a): lis = [int(ele) for ele in a.split()] head = None tail = None count = 0 for curele in lis: if curele == -1: break newnode = Node(curele) if head is None: head = newnode tail = newnode else: tail.next = newnode tail = newnode return head

 def printll(head): while head is not None: print(str(head.data) + "->", end="") head = head.next print("None")

 def length(head): # This function is used for determining the lenght of the Linked List c = 0 while head is not None: c += 1 head = head.next return c

 def appendN(head, n): count = length(head) - n #it will give me the index of first last digit we want to break and append on first. i = 0 tail = head #we don't want to change head so, we made a tail which will keep increasing till i<count. if count < 0 or count > length(head): #if count less than 0 or greater than length print(f"Please enter the number between 0 to {length(head)}") return while i < count: #loop run until we reach previous of the first last no. we want to append tail = tail.next i += 1 headT = tail #as we find that number we assigned it to new head tail.next = None #this tail.next is of head so we make it None while headT.next is not None: # we will make loop and assign the head to the last numbers.next headT = headT.next headT.next = head return headT

 lis = "9 8 7 7 6 5 4 3 3 2 1" head = typeinput(lis) c = appendN(head, 3) printll(c)

我希望你能明白我在这里想要做什么

您可能想尝试其他实现,如下所示。 请注意,即使使用负 integer n值,此代码也有效,因此如果您输入 -3 作为参数,LinkedList 将向相反方向移动,从而避免问题和异常:

 def appendN(head, n): if not head: return size = 1 tail = head while tail.next: size += 1 tail = tail.next tail.next = head for i in range(-n % size): tail = tail.next head = tail.next tail.next = None return head

您可以通过跟踪您在列表中一起推进的两个节点引用来执行您正在尝试的操作(我将其称为右旋转,而不是“追加”), n节点分开。

 def appendN(head, n): tail = new_tail = head # initialize two references into the list for _ in range(n): # advance the first one n times if not tail.next: raise ValueError("Linked list is too short") # or print and return tail = tail.next while tail.next: # then advance both in lockstep until you reach the end tail = tail.next new_tail = new_tail.next tail.next = head # link the end of the list to the beginning head = new_tail.next # grab a reference to the new head new_tail.next = None # and split the list where the second reference ended up return head

暂无
暂无

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

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