Straight-forward python solution with a nested list and for loop.


  • 0
    K

    Google offers me a candy again. I have to peel off the package.

    The so-called zig-zag or cyclic list is basically a "batch" visiting of all lists from left to right.

    The core part is to stack all lists into a nested list.

    class ZigzagIterator(object):
    
        def __init__(self, v1, v2):
            """
            Initialize your data structure here.
            :type v1: List[int]
            :type v2: List[int]
            """
            self.bigdata = [v1,v2]        
            self.length = len(v1)+len(v2)
            bigind = 0
            self.ver = []
            for bigind in xrange(max([len(x) for x in self.bigdata])):
                for i in self.bigdata:
                    if len(i)>bigind:
                        print i[bigind]
                        self.ver.append(i[bigind])
            print self.ver
    
        def next(self):
            """
            :rtype: int
            """
            self.length-=1
            return self.ver.pop(0)
            
    
        def hasNext(self):
            """
            :rtype: bool
            """
            return self.length>0
            
    
    # Your ZigzagIterator object will be instantiated and called as such:
    # i, v = ZigzagIterator(v1, v2), []
    # while i.hasNext(): v.append(i.next())
    

Log in to reply
 

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