Various problem sets of linked list Python

1. Single chain table data structure (each subsequent program needs to define the single chain table structure at the beginning, so I will omit not to write repeatedly)

class LNode(object):
    def __new__(self,x):
        #Data domain
        self.data =x
        #Link domain
        self.next =None
2. Build a single chain table according to the input utilization cycle. For the problem that the input data is not a chain table, it is assumed that the input is a list
#Create linked list
def constructListNode(array):
    if len(array) == 0:
        return None
    head = Node()
    head.val = array[0]
    # Temporary header node
    res = head
    for i in range(1, len(array)):
        temp = Node()
        temp.val = array[i]
        #Point to next node
        head.next = temp
        #Move to next node
        head = temp
    return res

3. Traverse the list

# Ergodic single chain table
def ergodicListNode(head):
    while head:
        print(head.val, end = ' ')
        head = head.next

4. Reverse list

#Inverted list
def reverseListNode(head):
    if not head or not head.next:
        return None
    #Build the front node
    pre =head
    #Build current node
    cur =head.next
    #Store the next node of the current node
    next = Node()
    while cur:
        #Store next node
        next =cur.next
        #Reverse, change direction
        cur.next = pre
        #Move backward, continue to reverse
        pre =cur
        cur =next
    head.next =None
    return pre

5. Access the K-th node in the linked list

#Access the K-th node in the linked list
def findK(head,k):
    cur = head
    while k>1:
        cur = cur.next
        k-=1
    return cur

6. Add elements at the front of the list

#Add add element in front of linked list
def add(value,head):
    #Create a node
    newnode = Node(value,None)
    #Add in front of linked list
    newnode.next = head
    head = newnode
    return head

7. Add elements at the end of the list

#Add elements at the end of the list
def addtail(value,head):
    #When the linked list is empty, the added element is set as the first element
    if head is None:
        head = Node(value,None)
    #Create a new node
    newcode = Node(value,None)
    #Prefix node
    pre =head
    #current node
    cur =head.next
    #Loop through to the end. When cur points to None, pre is the last node of the list
    while cur:
        pre = cur
        cur = cur.next 
    #Add element at the end   
    pre.next =newcode
    return head

8. Search whether an element is in the linked list

#Search whether an element is in the linked list
def  search(head,m):
    cur = head
    while cur:
        if m == cur.val:
            return True
        else:
            cur =cur.next
    return False

9. Delete an element in the linked list

#Calculate chain length
def Nodelen(head):
    count=0
    while head:
        count +=1
        head =head.next
    return count
#Delete an element in a linked list
def remove(head,n):
    #Empty linked list
    if head is None:
        return 0
    #The range of n exceeds the length of the list or is negative
    if n<0 or n>Nodelen(head):
        return 0
    #Find node with index n
    cur=head
    while n>1:
        pre =cur
        cur =cur.next
        n-=1
    #Find the nth node and delete the current node
    pre.next = cur.next
    cur=None
    return head

 

Posted by thisisnuts123 on Wed, 27 Nov 2019 14:12:37 -0800