Simply Python solution using zip function


  • 0
    K
    class ZigzagIterator(object):
    
        def __init__(self, v1, v2):
            """
            Initialize your data structure here.
            :type v1: List[int]
            :type v2: List[int]
            """
            self.idx = 0
            self.data = [x for pair in zip(v1, v2) for x in pair]
            if len(v1) > len(v2):
                self.data.extend(v1[len(v2):])
            else:
                self.data.extend(v2[len(v1):])
    
        def next(self):
            """
            :rtype: int
            """
            if self.hasNext():
                res = self.data[self.idx]
                self.idx += 1
                return res
            else:
                return None
            
    
        def hasNext(self):
            """
            :rtype: bool
            """
            return self.idx < len(self.data)
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.