简体   繁体   English

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

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

I am learning linked lists by doing assignments, and the code for making a Linked list is working fine.我通过做作业来学习链表,制作链表的代码运行良好。 But, manipulating it is not working properly.但是,操纵它不能正常工作。 So, I have a Linked list with a head and I created a function that takes two arguments (head, how many the last numbers you want to append in the first).所以,我有一个带头的链接列表,我创建了一个 function ,它需要两个 arguments (头,你想在第一个 append 的最后几个数字)。 The purpose of the function is, suppose you have a linked list. function 的目的是,假设你有一个链表。 1 -> 4->5->6->2->3->9 I want the last three numbers to be on the first and the remaining first 4 numbers to come at the last of these three numbers like this. 1 -> 4->5->6->2->3->9 我希望最后三个数字出现在第一个数字上,剩下的前四个数字出现在这三个数字的最后一个数字上,就像这样。 2->3->9->1 -> 4->5->6 So, what should I need is to have broken the link between 2 and its previous number(ie, 6) and link 6.next to None and then connect 9.next to 1, to get the desired result. 2->3->9->1 -> 4->5->6 所以,我需要断开 2 与其前一个数字(即 6)之间的链接,并将 6.next 链接到 None 和然后将 9.next 连接到 1,以获得所需的结果。 But, I am having some issues in the code it is running infinite time and a little bit wrong.但是,我在运行无限时间的代码中遇到了一些问题,而且有点错误。 Can you please take a look into this and help me?你能看看这个并帮助我吗? Thank you谢谢

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)

I hope you get the Idea what I am trying to do here我希望你能明白我在这里想要做什么

You might want to try another implementation like the following.您可能想尝试其他实现,如下所示。 Note that this code works even with negative integer n values, so if you enter -3 as a parameter, the LinkedList will shift on the opposite direction, avoiding issues and exceptions:请注意,即使使用负 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

You can do the operation you're attempting (which I would call a right-rotation, not an "append"), by keeping track of two node references that you advance together through the list, n nodes apart.您可以通过跟踪您在列表中一起推进的两个节点引用来执行您正在尝试的操作(我将其称为右旋转,而不是“追加”), 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