The repressed emotion will not disappear, but will erupt in a more ugly way after it has accumulated to a certain extent, which is the cause of some mental illness. If we are blindly depressed and cannot sublimate our indignation, our self-evaluation will be declining. —— Seven Habits of Highly Effective People
This passage tells us that we must say something in our hearts, find someone to talk about it, or write it out. Or if there are too many bug s, they will be blown up and even smashed the broken computer... Let's study hard.
Topic: Given a single list of leading nodes: head - > 1 - > 2 - > 3 - > 4 - > 5 - > 6 - > 7 - > 8; make it head - > 8 - > 7 - > 6 - > 5 - > 4 - > 3 - > 2 - > 1.
Today's topic is to reverse the list of single links. I have already written two ways before. One is an article. In-situ reverse sequence method One article Recursive method . In-situ inverse order method is to reverse the traversed nodes. It needs to traverse the entire list once, and the time complexity is O(n). It needs constant additional variables to save the precursor and successor nodes of the current node, so the space complexity is O(1).
The performance of recursive method is slightly worse than that of in-situ inverse method. The advantage is that the idea is intuitive and easy to understand; but the disadvantage is that it is difficult to implement. Another point is that the recursive depth supported by Python is 1000 by default. In yesterday's code, I added the length of the list to 998, which is the limit, and will report the error in the following figure.

After searching for data, this is a mechanism specially designed by Python to prevent Python overflow crash caused by infinite recursion. The default depth can be changed:
import sys sys.setrecursionlimit(1500) # This is to change the recursive depth to 1500
Okay, today's method is insertion.
The main idea of insertion method is to start traversing from the second node of the list, insert the traversing current node behind the head node, until all the nodes are traversed. That is, the original list is head - > 1 - > 2 - > 3 - > 4 - > 5 - > 6 - > 7 - > 8. From the beginning of traversal, insert it in front of 1, that is, after the head node: head - > 2 - > 1 - > 3 - > 4 - > 5 - > 6 - > 7 - > 8, then traverse to 3, and insert it behind the head node. When all nodes are inserted behind the head of the header node, the reverse order of the list can be achieved.
Of course, we have to have a list in reverse order. The construction method of the list was written in yesterday's article. I can't understand it.—— python algorithm-002 single linked list reverse recursion method The following code implements:
#First construct node objects class LNode: """ //Define linked list objects """ def __init__(self, x): self.data = x self.next = None #Constructing linked list function def creatLink(x): i = 1 head = LNode(None) tmp = None cur = head while i <= x: tmp = LNode(i) cur.next = tmp cur = tmp i += 1
With the linked list, the next step is to implement the algorithm:
def Reverse(head): ''' //Lead node //Input: Link List //Output: Inverse linked list ''' #First determine whether the list is empty or if there is only one element, and then return if head is None or head.next is None: return head cur = None # Used to traverse linked lists next = None # Successor nodes used to point to the current node cur = head.next.next # Traverse from the second node of the list head.next.next= None # Disconnect the first node from the second node. # Note that this is the point! Be sure to disconnect, because if you keep opening, in the next step # The next domain of the second node is the first node, but the next domain of the first node is the second section. # Point, this becomes the smallest ring, and then the next program goes into a dead cycle. while cur is not None: next=cur.next # next is used to save the successor nodes of cur, otherwise the latter nodes will be lost cur.next=head.next # Insert the currently traversed node after the header node head.next = cur #Here are two steps to achieve "insertion", detailed experience cur = next # Traveling through the next node return head
This is the end of the algorithm. Now let's try this algorithm.
if __name__=='__main__': # Construct linked list head=creatLink(8) #Print the list before reverse order print("BeforeReverse:") cur = head.next while cur != None: print(cur.data) cur = cur.next print("\nAfterReverse:") #Calling an inverse function head=Reverse(head) #Print the list in reverse order cur = head.next while cur != None: print(cur.data) cur = cur.next
This is the result of operation:

Give me all the code:
#First construct node objects class LNode: """ //Define linked list objects """ def __init__(self, x): self.data = x self.next = None #Constructing linked list function def creatLink(x): i = 1 head = LNode(None) tmp = None cur = head while i <= x: tmp = LNode(i) cur.next = tmp cur = tmp i += 1 def Reverse(head): ''' //Lead node //Input: Link List //Output: Inverse linked list ''' #First determine whether the list is empty or if there is only one element, and then return if head is None or head.next is None: return head cur = None # Used to traverse linked lists next = None # Successor nodes used to point to the current node cur = head.next.next # Traverse from the second node of the list head.next.next= None # Disconnect the first node from the second node. # Note that this is the point! Be sure to disconnect, because if you keep opening, in the next step # The next domain of the second node is the first node, but the next domain of the first node is the second section. # Point, this becomes the smallest ring, and then the following program goes into a dead cycle. while cur is not None: next=cur.next # next is used to save the successor nodes of cur, otherwise the latter nodes will be lost cur.next=head.next # Insert the currently traversed node after the header node head.next = cur #Here are two steps to achieve "insertion", detailed experience cur = next # Traveling through the next node return head if __name__=='__main__': # Construct linked list head=creatLink(8) #Print the list before reverse order print("BeforeReverse:") cur = head.next while cur != None: print(cur.data) cur = cur.next print("\nAfterReverse:") #Calling an inverse function head=Reverse(head) #Print the list in reverse order cur = head.next while cur != None: print(cur.data) cur = cur.next
Okay, today's algorithm is written on a list of headed nodes. Would you like it without headed nodes? Welcome to discuss, in my github There are also codes on it. You can check them. You can also send your code to me and trust me. You can also pay attention to my own public number: DKider to contact me.
Let's cheer together.
Mechanics is like plowing hard, diligent and lazy. But if there are many kinds of books, there will be years to come. —— Song Liu Guo's Academy