Offer to prove safety program title (python) - list

1, the print head from the end of the list

# Input a list, the value returned by a ArrayList list sequentially from the tail to the head. 
class
ListNode: DEF the __init__ (Self, X): self.val = X self.next = None class Solution: DEF printListFromTailToHead (Self, listnode): # Write code here Wallpaper L = [] head = listnode the while head: l.insert ( 0, head.val) # insertion head = head.next return L

2, the list penultimate nodes K

# Input a linked list, the linked list output reciprocal k-th node. 
class
Solution: DEF FindKthToTail (Self, head, K): # Write code here Wallpaper L = [] the while head =! None: l.append (head) head = head.next IF K> len (L) or K <. 1 : return return L [-k]

3, the reverse list

# Input a linked list, the inverted list, the new list of the output header. 
class
Solution: # Returns ListNode DEF ReverseList (Self, PHEAD): # Write code here Wallpaper L = [] IF PHEAD == None: return the while PHEAD: l.insert (0, PHEAD) PHEAD = pHead.next for I in Range ( len (L) -1 ): L [I] .next = L [I +. 1 ] L [ -1] .next = None return L [0]

4, merging two sorted lists

# Two monotonically increasing input list and output list after synthesis of two lists, of course, after that we need to meet synthesis list - decreasing the rules. 
class
Solution: DEF the Merge (Self, pHead1, pHead2): L1 = [] L2 = [] the while pHead1: l1.append (pHead1) pHead1 = pHead1.next the while pHead2: l2.append (pHead2) pHead2 = pHead2.next L + L1 = L2 IF L == []: return None for I in Range (len (L) -1): for j in range(i+1,len(l)): if l[i].val>l[j].val: l[i],l[j] = l[j],l[i] for t in range(len(l)-1): l[t].next= l[t+1] l[-1].next =None return l[0]

5, Copy to copy the list

# A complex input list (each node has a node value, and two pointers, a pointer to the next node, a special pointer to any other node), # 
head returns a value of complex linked list after copying. (Note that the output results do not return parameter node reference, otherwise the program will return to direct questions sentenced empty)
class
RandomListNode:
def __init__(self, x):
        self.label = x
        self.next = None
        self.random = None
class Solution:
    # 返回 RandomListNode
    def Clone(self, pHead):
        # write code here
        if not pHead:
            return None
        cur = pHead
        while cur:
            tmp = RandomListNode(cur.label)
            tmp.next = cur.next
            cur.next =tmp 
            contribute = tmp.next 
        contribute = Pet
         While adding: 
            tmp = cur.next
             IF cur.random: 
                tmp.random = cur.random.next 
            contribute = tmp.next 
        contribute = Pet 
        RES = pHead.next
         While cur.next: 
            tmp = cur.next 
            cur.next = tmp.next 
            contribute = tmp
         return res 

6, binary search trees and doubly-linked list

# Enter a binary search tree, converts the binary search tree into a sort of two-way linked list. 
# Requirements can not create any new node, point to only adjust the tree node pointer. 

class Solution:
     DEF the Convert (Self, pRootOfTree):
         # Write code here Wallpaper 
        IF  Not pRootOfTree: return 
        self.arr = [] 
        self.midTraversal (pRootOfTree) 
        for I, V in the enumerate (self.arr [: -. 1 ]): 
            V .right = self.arr [I +. 1 ] 
            self.arr [I +. 1] .left = V
         return self.arr [0] 
 
    DEF midTraversal (Self, the root):
         IF  Not root: return
        self.midTraversal(root.left)
        self.arr.append(root)
        self.midTraversal(root.right)

7, a first common node of the two lists

# Enter the two lists to find their first common node. 
class
Solution: DEF FindFirstCommonNode (Self, pHead1, pHead2): # Write code here Wallpaper L1 = [] Result = [] the while pHead1: l1.append (pHead1.val) pHead1 = pHead1.next the while pHead2: IF pHead2.val in L1 : result.append (pHead2) pHead2 = pHead2.next IF Result == []: return None else: return result[0]

8, the list entry ring node

# To a linked list, wherein if the ring comprises, find the entry ring node list, otherwise, outputs null. 
class
Solution: DEF EntryNodeOfLoop (Self, PHEAD): # Write code here Wallpaper L = [] the while PHEAD: l.append (PHEAD) PHEAD = pHead.next IF PHEAD in L: return PHEAD

9, remove the duplicate node list

# In a sorted linked list nodes duplicate, delete the duplicate node list, the node does not retain repeated, returns the head pointer list. 
# For example, list 1-> 2-> 3-> 3-> 4-> 4-> 5 workup 1-> 2-> 5
#: first traversal, so that the list -> list, the list is repeated removed elements of the new list reconstruction list
class
Solution: DEF deleteDuplication (Self, PHEAD): Vals = [] Nodes = [] the while PHEAD: vals.append (pHead.val) nodes.append (PHEAD) PHEAD = pHead.next Vals List = (filter ( the lambda C: vals.count (C) ==. 1 , Vals)) Nodes = List (filter ( the lambda D: d.val in vals, nodes)) if nodes== []: return None for i in range(len(nodes)-1): nodes[i].next = nodes[i+1] nodes[-1].next =None return nodes[0]

 

Guess you like

Origin www.cnblogs.com/fighting25/p/11265271.html