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 reverse python single linked list

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

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

The code is as follows:

Class Node (object): def _ _ init__ (self, elem) Next_=None): self.elem = elem self.next = next_ def reverseList (head): if head = = None or head.next==None: # if the linked list is empty or there is only one number, return return head pre = None next= None while (head! = None): next= head.next # 1 head.next= pre # 2 pre = head # 3 Head = next # 4 return preif _ name__ ='_ main__': L1 = Node (3) # create the linked list 3-> 2-> 1-> 9-> None l1.next = Node (2) l1.next.next = Node (1) l1.next.next.next = Node (9) l = reverseList (L1) print (l.elem L.next.elem, l.next.next.elem, l.next.next.next.elem)

Original single linked list:

Inverted single linked list:

The reversal process is as follows:

Step 1: next = head.next

Assign head.next to the next variable, that is, next points to node 2, and save node 2 first.

Step 2: head.next = pre (initial pre==None)

Assign the pre variable to head.next, that is, node 1 points to None

Step 3: pre = head

Head is assigned to pre, that is, pre points to node 1 and node 1 is set to "previous node"

Step 4: head = next

Assign next to head, that is, head points to node 2, and node 2 is set to "header node"

After the first cycle, enter the second cycle, as shown in the following figure:

Step 1: next = head.next

Assign head.next to the next variable, that is, next points to node 3, and save node 3 first.

Step 2: head.next = pre (pre is no longer None at this time)

Assigning pre to head.next,pre points to node 1 in the last loop, so the meaning of this step is that node 2 points to node 1, completing the reversal of nodes 1 and 2.

Step 3: pre = head

Head is assigned to pre, that is, pre points to node 2 and node 2 is set to "previous node"

Step 4: head = next

Assign next to head, that is, head points to node 3. At this point, node 3 is set to "head node"

The second cycle is over, and so on! The third, the fourth, the fifth cycle. Finally, it is inverted to the following figure

Several points for attention:

(1) help memorize the map:

(2) the next node of the current head node must be saved (for example, if the current head node is 2, save node 3 first)

(3) key point: head.next = pre that implements the inversion

At this point, the study on "how to reverse the single linked list of python" 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: 283

*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