-4

I m trying to have this result: [5,4,8]

but I m getting this: []

listA = [4,1,8,4,5]
listB = [5,6,1,8,4,5]

class Solution(object):
    
    def getIntersectionNode(listA,listB):
        de=[]
        for i in range(-1,-len(listB)): 
            for j in range(-1,-len(listB)):
                
                if listA[i] == listB[i]:de.append(listA[-1])
                
                elif listA[-1] != listB[-1]: print("null")  
                                          
                
        return de   

What is wrong with this code?

M A
  • 1
  • 3

2 Answers2

0

If you don't need to worry about duplicates:

listA = [4,1,8,4,5]
listB = [5,6,1,8,4,5]

class Solution(object):
    
    def getIntersectionNode(listA,listB):
        de=list(set(listA) & set(listB))
             
        return de   
print(Solution.getIntersectionNode(listA,listB))
chess_lover_6
  • 632
  • 8
  • 22
  • it has to start from end of the list and must stop when it differs(in this case it is just 5,4,8) – M A Mar 04 '21 at 20:30
0

The intersection is not [8, 1, 4, 5]?

If what you want is the intersection, you could use sets to achieve that easily:

list(set(listA).intersection(set(listB)))