Given a matrix of size N x N. Print the elements of the matrix in the snake like pattern depicted below.
Example 1:
Input:
N = 3 
matrix[][] = {{45, 48, 54},
             {21, 89, 87}
             {70, 78, 15}}
Output: 45 48 54 87 89 21 70 78 15 
Explanation:
Matrix is as below:
45 48 54
21 89 87
70 78 15
Printing it in snake pattern will lead to 
the output as 45 48 54 87 89 21 70 78 15.
Example 2:
Input:
N = 2
matrix[][] = {{1, 2},
              {3, 4}}
Output: 1 2 4 3
Explanation:
Matrix is as below:
1 2 
3 4
Printing it in snake pattern will 
give output as 1 2 4 3.
Your Task:
You dont need to read input or print anything. Complete the function snakePattern() that takes matrix as input parameter and returns a list of integers in order of the values visited in the snake pattern. 
Expected Time Complexity: O(N * N)
Expected Auxiliary Space: O(N * N) for the resultant list only.
Constraints:
1 <= N <= 100
1 <= mat[i][j] <= 100

Difficulty: EASY
Tags: Matrices

Data structures
Skill types: Data structures
Expected Time Complexity: O(N * N)
Expected Auxiliary Space: O(N * N) for the resultant list only.

Starter code:

#User function Template for python3

class Solution:
    
    #Function to return list of integers visited in snake pattern in matrix.
    def snakePattern(self, matrix): 
       # code here 

Solutions:
class Solution:

	def snakePattern(self, matrix):
		l = []
		for i in range(len(matrix)):
			if i % 2 == 0:
				l += matrix[i]
			else:
				l += matrix[i][::-1]
		return l


class Solution:

	def snakePattern(self, matrix):
		ls = []
		n = len(matrix)
		for i in range(n):
			if i % 2 == 0:
				for j in range(n):
					ls.append(matrix[i][j])
			else:
				for j in range(n - 1, -1, -1):
					ls.append(matrix[i][j])
		return ls


class Solution:

	def snakePattern(self, matrix):
		l = []
		i = 0
		j = 0
		for i in range(len(matrix[0])):
			if i % 2 == 0:
				for j in range(len(matrix[0])):
					l.append(matrix[i][j])
			else:
				j = len(matrix[0]) - 1
				while j >= 0:
					l.append(matrix[i][j])
					j = j - 1


class Solution:

	def snakePattern(self, matrix):
		k = []
		x = len(matrix)
		for i in range(x):
			if i % 2 == 0:
				for j in range(x):
					k.append(matrix[i][j])
			else:
				for j in range(x - 1, -1, -1):
					k.append(matrix[i][j])
		return k


class Solution:

	def snakePattern(self, matrix):
		n = len(matrix)
		result = []
		for i in range(n):
			if i % 2:
				result.extend(matrix[i][::-1])
			else:
				result.extend(matrix[i][:])
		return result


class Solution:

	def snakePattern(self, matrix):
		result = []
		a = len(matrix)
		b = a
		for i in range(a):
			if i % 2 == 0:
				for j in range(b):
					result.append(matrix[i][j])
			else:
				for j in range(b - 1, -1, -1):
					result.append(matrix[i][j])
		return result


class Solution:

	def snakePattern(self, matrix):
		turn = 0
		N = len(matrix)
		result = []
		for i in range(N):
			if i % 2 == 0:
				for j in range(N):
					result += [matrix[i][j]]
			else:
				for j in range(N - 1, -1, -1):
					result += [matrix[i][j]]
		return result


class Solution:

	def snakePattern(self, matrix):
		ans = []
		n = len(matrix)
		flag = True
		for i in range(n):
			if flag:
				(start, end, inc) = (0, n, 1)
				flag = False
			else:
				(start, end, inc) = (n - 1, -1, -1)
				flag = True
			for j in range(start, end, inc):
				ans.append(matrix[i][j])
		return ans


class Solution:

	def snakePattern(self, a):
		n = len(a)
		for i in range(n):
			if i % 2 == 0:
				continue
			else:
				(left, right) = (0, n - 1)
				while left < right:
					temp = a[i][left]
					a[i][left] = a[i][right]
					a[i][right] = temp
					left += 1
					right -= 1
		ans = []
		for i in range(n):
			for j in range(n):
				ans.append(a[i][j])
		return ans


class Solution:

	def snakePattern(self, m):
		res = []
		for i in range(len(m)):
			if i % 2 == 0:
				for j in range(len(m)):
					res.append(m[i][j])
			else:
				for j in range(len(m) - 1, -1, -1):
					res.append(m[i][j])
		return res


class Solution:

	def snakePattern(self, matrix):
		n = len(matrix)
		li = []
		for i in range(n):
			if i % 2 == 0:
				for j in range(n):
					li.append(matrix[i][j])
			else:
				for j in reversed(range(n)):
					li.append(matrix[i][j])
		return li


class Solution:

	def snakePattern(self, m):
		a = []
		l = len(m)
		for i in range(l):
			if i % 2 == 0:
				p = 1
			else:
				p = -1
			a += m[i][::p]
		return a


class Solution:

	def snakePattern(self, matrix):
		l = []
		n = len(matrix[0])
		for j in range(n):
			if j % 2 == 0:
				for k in range(n):
					l.append(matrix[j][k])
			else:
				for k in range(n - 1, -1, -1):
					l.append(matrix[j][k])
		return l


class Solution:

	def snakePattern(self, matrix):
		i = 0
		j = 0
		ans = []
		for i in range(n):
			if i % 2 == 0:
				for j in range(n):
					ans.append(matrix[i][j])
			if i % 2 != 0:
				for j in range(n - 1, -1, -1):
					ans.append(matrix[i][j])
		return ans


import math

class Solution:

	def snakePattern(self, matrix):
		arr = []
		row = int(math.sqrt(len(matrix)))
		for i in range(len(matrix)):
			if i % 2 == 0:
				for j in range(len(matrix[0])):
					arr.append(matrix[i][j])
			else:
				for j in range(len(matrix[0]) - 1, -1, -1):
					arr.append(matrix[i][j])
		return arr


class Solution:

	def snakePattern(self, matrix):
		N = n
		l = []
		for i in range(N):
			if i % 2 == 0:
				for j in range(N):
					l.append(matrix[i][j])
			else:
				for j in range(N - 1, -1, -1):
					l.append(matrix[i][j])
		return l


class Solution:

	def snakePattern(self, matrix):
		t = len(matrix)
		n = len(matrix)
		l = []
		for i in range(t):
			if i % 2 == 0:
				for j in range(n):
					l.append(str(matrix[i][j]))
			else:
				for j in range(n - 1, -1, -1):
					l.append(str(matrix[i][j]))
		return l


class Solution:

	def snakePattern(self, matrix):
		N = len(matrix)
		ans = []
		for i in range(N):
			if i % 2 == 0:
				for j in range(N):
					ans.append(matrix[i][j])
			else:
				for j in range(N - 1, -1, -1):
					ans.append(matrix[i][j])
		return ans


class Solution:

	def snakePattern(self, matrix):
		li = []
		for i in matrix:
			if matrix.index(i) % 2 == 0:
				for j in i:
					li.append(j)
			else:
				for k in i[::-1]:
					li.append(k)
		return li


class Solution:

	def snakePattern(self, M):
		l = []
		for i in range(len(M)):
			if i % 2 == 0:
				for j in range(len(M)):
					l.append(M[i][j])
			else:
				for k in range(len(M) - 1, -1, -1):
					l.append(M[i][k])
		return l


global n

class Solution:

	def snakePattern(self, m):
		arr = []
		for i in range(n):
			if i % 2 == 0:
				for j in range(n):
					arr += [m[i][j]]
			else:
				for j in range(n - 1, -1, -1):
					arr += [m[i][j]]
		return arr


class Solution:

	def snakePattern(self, matrix):
		l = []
		p = len(matrix)
		q = len(matrix[0])
		for i in range(p):
			if i % 2 == 0:
				for j in range(q):
					l.append(matrix[i][j])
			else:
				for j in range(q - 1, -1, -1):
					l.append(matrix[i][j])
		return l


class Solution:

	def snakePattern(self, matrix):
		(r, c) = (len(matrix), len(matrix[0]))
		res = []
		for i in range(r):
			if i % 2 == 0:
				for j in range(0, c):
					res.append(matrix[i][j])
			else:
				for j in range(c - 1, -1, -1):
					res.append(matrix[i][j])
		return res


class Solution:

	def snakePattern(self, matrix):
		a = []
		flag = True
		for i in range(n):
			if flag == True:
				for j in range(n):
					a.append(matrix[i][j])
			else:
				for j in range(n - 1, -1, -1):
					a.append(matrix[i][j])
			flag = not flag
		return a


class Solution:

	def snakePattern(self, mat):
		n = len(mat)
		change = False
		res = []
		for i in range(n):
			if change:
				for j in range(n - 1, -1, -1):
					res.append(mat[i][j])
			else:
				for j in range(n):
					res.append(mat[i][j])
			change = not change
		return res


class Solution:

	def snakePattern(self, matrix):
		k = matrix[0]
		if n > 1:
			for i in range(1, n):
				if i % 2 == 1:
					p = matrix[i]
					p.reverse()
					k = k + p
				elif i % 2 == 0:
					k = k + matrix[i]
			return k
		else:
			return matrix[0]


class Solution:

	def snakePattern(self, matrix):
		R = len(matrix)
		C = len(matrix[0])
		l = []
		for i in range(R):
			if i % 2 == 0:
				for j in range(C):
					l.append(matrix[i][j])
			else:
				for j in range(C - 1, -1, -1):
					l.append(matrix[i][j])
		return l


class Solution:

	def snakePattern(self, matrix):
		lst = []
		row = len(matrix)
		col = len(matrix[0])
		for i in range(row):
			if i % 2 == 0:
				for j in range(col):
					lst.append(matrix[i][j])
			else:
				for j in range(col - 1, -1, -1):
					lst.append(matrix[i][j])
		return lst


class Solution:

	def snakePattern(self, matrix):
		c = []
		for i in range(len(matrix)):
			if i % 2 == 0:
				for j in range(len(matrix)):
					c.append(matrix[i][j])
			elif i % 2 != 0:
				for k in range(len(matrix) - 1, -1, -1):
					c.append(matrix[i][k])
		return c