An alternative python method

  • 0

    Pop the next item whenever needed.

    def __init__(self, v1, v2):
        self.v1 = v1
        self.v2 = v2
    def next(self):
        if self.v1 or self.v2:
            if self.v1:
                res = self.v1.pop(0)
                if self.v2:
                    self.v1, self.v2 = self.v2, self.v1
                res = self.v2.pop(0)
                self.v1, self.v2 = self.v2, self.v1
            return res
        return []
    def hasNext(self):
        return len(self.v1) + len(self.v2) > 0

Log in to reply

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