Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python Recursion to realize Fast inversion of linked list

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use python recursion to achieve fast list reversal". In daily operation, I believe that many people have doubts about how to use python recursion to achieve fast list reversal. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for everyone to answer the doubt of "how to use python recursion to achieve fast list reversal". Next, please follow the editor to study!

Case study: reverse the following linked list

Source code:

Node is used to represent nodes in a queue; it contains two domains. Val represents the value of the node. Next points to the next node''# defines the data structure of the linked list class Node: def _ init__ (self,val): self.next = None self.val = valclass ListUtility:# generates a linked list for operation def _ _ init__ (self): self.head = None self.tail = None pass def createList (self,nodeNum): if nodeNum 0: # if the head pointer is empty The code first constructs the queue header, if it is not empty, the code constructs the node object, and then uses the next pointer of the previous node to point to the current node, thus concatenating multiple nodes into a queue. If head is None: head = Node (val) node = head else: node.next = Node (val) node = node.next self.tail = nodeval + = 1 nodeNum-= 1 self.head = head return head def printList (self Head): while head is not None: print ("{0}->" .format (head.val), end = "") head = head.next print ("null") class ListReverse: def _ _ init__ (self, head): self.listHead = head self.newHead = None def recursiveReverse (self Node): # if the queue is empty or there is only one node Then the queue has been reversed and completed if node is None or node.next is None: self.newHead = node return node''if the queue contains multiple nodes, then all nodes after the current node are reversed first by recursive call Then point the next pointer of the node after the current node to yourself to complete the whole list of all nodes resulting in''head = self.recursiveReverse (node.next) head.next = node node.next = None return node def getReverseList (self):' 'listHead is the original queue header node, and the newHead points to the header node of the new list after recursiveReverse It actually corresponds to the tail node of the original list, while head points to the tail node of the new list,''self.recursiveReverse (self.listHead) return self.newHead utility = ListUtility () head = utility.createList (10) utility.printList (head) # to perform the inversion algorithm, and then print the queue again to see if it leads to success reverse = ListReverse (head) utility.printList (reverse.getReverseList ()).

Running result:

At this point, the study on "how to use python recursion to achieve fast reversal of linked lists" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report