The sword refers to Offer (3): print the linked list from end to end

                       The sword refers to Offer (3): print the linked list from end to end

PS: personal notes. According to Jack-Cui's blog learning, C++ has learned to make up.

Topic description:

Input a linked list , return a reversed linked list .

Ideas:

Usually, in this case, we do not want to modify the structure of the original linked list. Returns a linked list in reverse order, this is the classic "last in first out" , we can use stack to achieve this order. When passing through a node, put the node on a stack (put the node at the bottom of the stack first). After traversing the entire linked list, output the values ​​of the nodes one by one from the top of the stack (the value at the top of the stack is the last one, so that the effect of last-in, first-out is achieved), and give a new linked list structure, so that the linked list is reversed.

The linked list is the most basic and commonly used data structure. The singly linked list in the C++ language is implemented by using pointer operations. As an object-oriented programming in python, you can create a Node class to implement the linked list, and use the attribute reference of the class to replace the pointer operation.

Code:

# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
 
class Solution: #⭐This code gives an error after trying
    def printListFromTailToHead(self, listNode):
        # write code here
        result = []
        while listNode:
            result.insert(0, listNode.val)
            listNode = listNode.next
        return result

Another blogger's code:

class ListNode(): #Construction of linked list, initialization
    def __init__(self, x):  
        self.val = x  
        self.next = None  
  
def CreateList(n): #Create a linked list
    if n <= 0: #no node reports an error
        return False  
    if n == 1: #only one node
        return ListNode(1)  
    else:  
        listNode = ListNode(1)  
        tmp = listNode  
        for i in range(2,n+1): #Add nodes one by one
            tmp.next = ListNode(i)  
            tmp  = tmp.next  
    return listNode #return the root node
  
def PrintList(listNode): #Used to print linked list nodes  
    tmp = listNode #Do not change the original linked list  
    while tmp:  
        print(tmp.val)  
        tmp = tmp.next  
          
def function1(listNode):  
    lists = []  
    while listNode:  
        lists.append(listNode.val)  
        listNode = listNode.next  
    return lists[::-1] #The step size is set to -1, which is reversed
  
def function2(listNode):  
    lists = []  
    while listNode:  
        lists.append(listNode.val)  
        listNode = listNode.next  
    lists.reverse() #Directly use a table method
    return lists   

def main():  
    listNode = CreateList(11) #Print the linked list for testing  
    PrintList(listNode)  
    print(function1(listNode))
      
  
if __name__ == "__main__":  
    main()  


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324684496&siteId=291194637