VIVO 笔试编程题准备

'''
给A,B两个数组,
输出出现在A中而没有出现在B中的元素,输出元素前后顺序不变
'''
if __name__=="__main__":
    array_A=list(map(lambda x:int(x),input().strip().split()))
    array_B=list(map(lambda x:int(x),input().strip().split()))

    result=[]
    for i in range(len(array_A)):
        find=False
        for j in range(len(array_B)):
            if array_B[j]==array_A[i]:
                find=True
                break
        if find==False:
            result.append(array_A[i])
    print(result)
'''
反转链表的第M个至第N个元素
对链表进行反转:即改变链表中节点的next域
'''
class ListNode:
    def __init__(self,x):
        self.value=x
        self.next=None

def inverse_M_to_N(head,M,N):
    if M>=N or head==None or head.next==None:
        return head
    N=N-M
    new_head=ListNode(0)
    new_head.next=head
    prev=new_head
    curr=head
    while M>1:
        prev=prev.next
        curr=curr.next
        M-=1
    part1_end=prev
    part2_end=curr
    prev=prev.next
    curr=curr.next
    next=None
    while N>0:
        next=curr.next
        curr.next=prev

        prev = curr
        curr=next
        N-=1
    part3_start=next
    part2_start=prev
    part1_end.next=part2_start
    part2_end.next=part3_start

    return  new_head.next

if __name__=="__main__":
    a=[1,2,3,4,5,6]
    LinkList=ListNode(0)
    head=LinkList
    for i in range(len(a)):
        new_node=ListNode(a[i])
        head.next=new_node
        head=head.next

    inverse_head=inverse_M_to_N(LinkList.next,1,6)

    new_head=inverse_head
    while(new_head):
        print(new_head.value)
        new_head=new_head.next

 

你可能感兴趣的:(leetcode)