Given the adjacency list representation of an undirected graph
your task is to complete the function printGraph() that takes the number of vertices (V) and the adjacency list as input parameters. 
The function should return a new adjacency list, where each adjacency list contains the vertex itself at first position then all its adjacent nodes, in the same order of the given adjacency list of that vertex.
Example 1:
Input:
Output: 
0-> 1-> 4 
1-> 0-> 2-> 3-> 4 
2-> 1-> 3 
3-> 1-> 2-> 4 
4-> 0-> 1-> 3
Explanation:
As 0,1 and 3 is connected to 4 so 4th row
of the list containing 4 and its connected
nodes 0,1 and 3 and we have to add those in
sorted order and same for every row.
Example 2:
Input:
Output: 
0-> 2-> 3 
1-> 2 
2-> 0-> 1 
3-> 0
Explanation:
As 2 and 3 is connected to 0 so 0th row
of the list containing 0 and its connected 
nodes 2 and 3 and we have to add those in
sorted order and same for every row. 
Your task:
You dont need to read input or print anything. Your task is to complete the function printGraph() which takes the integer V denoting the number of vertices and adjacency list as input parameters and returns the list of list  contains the node itself at first position with its connected nodes(as same as it is given in input adjacency  list).
Expected Time Complexity: O(V + E)
Expected Auxiliary Space: O(V + E)
Constraints:
1 ≤ V, E ≤ 10^{4}

Source: geeksforgeeks | Difficulty: EASY
Tags: Graph algorithms

Data structures

Starter code: #User function Template for python3



class Solution:

    

    #Function to return the adjacency list for each vertex.

    def printGraph(self, V, adj):

        # code here

Solutions:
class Solution:

	def printGraph(self, V, adj):
		ans = []
		for i in range(V):
			temp = []
			temp.append(i)
			for j in adj[i]:
				temp.append(j)
			ans.append(temp)
		return ans


class Solution:

	def printGraph(self, V, adj):
		out = []
		for i in range(len(adj)):
			out.append([i] + adj[i])
		return out


class Solution:

	def printGraph(self, V, adj):
		count = 0
		for row in adj:
			row.insert(0, count)
			count += 1
		return adj


from copy import deepcopy

class Solution:

	def printGraph(self, V, adj):
		for i in range(len(adj)):
			adj[i] = list([i]) + adj[i]
		return adj


class Solution:

	def printGraph(self, V, adj):
		adj_list = []
		for i in range(len(adj)):
			arr = [i]
			for j in range(len(adj[i])):
				arr.append(adj[i][j])
			adj_list.append(arr)
		return adj_list


class Solution:

	def printGraph(self, V, adj):
		l = []
		for i in range(V):
			l.append([i])
		for i in range(V):
			for j in adj[i]:
				l[i].append(j)
		return l


class Solution:

	def printGraph(self, V, adj):
		new_list = [[] for i in range(V)]
		for i in range(len(adj)):
			new_list[i].append(i)
			for j in range(len(adj[i])):
				new_list[i].append(adj[i][j])
		return new_list


class Solution:

	def printGraph(self, V, adj):
		ans = []
		for i in range(V):
			sub = [i]
			sub.extend(adj[i])
			ans.append(sub)
		return ans


class Solution:

	def printGraph(self, V, adj):
		adjlist = [None] * V
		for i in range(V):
			adjlist[i] = [i]
			for j in range(len(adj[i])):
				adjlist[i].append(adj[i][j])
		return adjlist


class Solution:

	def printGraph(self, V, adj):
		adj_lst = []
		for i in range(len(adj)):
			temp = [i]
			for v in adj[i]:
				temp.append(v)
			adj_lst.append(temp)
		return adj_lst


class Solution:

	def printGraph(self, V, adj):
		visited = [0] * V
		adj_nodes = []
		for i in range(V):
			self.bfs(i, adj_nodes, adj)
		return adj_nodes

	def bfs(self, vertex, nodes, adj):
		lst = []
		for i in adj[vertex]:
			lst.append(i)
		lst.sort()
		lst.insert(0, vertex)
		nodes.append(lst)


class Solution:

	def printGraph(self, V, adj):
		adjacency_list = [[] for _ in range(V)]
		for v in range(V):
			adjacency_list[v] = [str(v)] + [str(node) for node in adj[v]]
		return adjacency_list


class Solution:

	def printGraph(self, V, adj):
		adj_list = []
		for i in range(V):
			adj_list.append([i] + adj[i])
		return adj_list


class Solution:

	def printGraph(self, V, adj):
		ans = []
		for i in range(len(adj)):
			temp = [i]
			for x in adj[i]:
				temp.append(x)
			ans.append(temp)
		return ans


import collections

class Solution:

	def printGraph(self, V, adj):
		edges = collections.defaultdict(list)
		for i in range(len(adj)):
			edges[i].append(i)
			for vertice in adj[i]:
				edges[i].append(vertice)
		return edges


class Solution:

	def printGraph(self, V, adj):
		res = [[i] for i in range(V)]
		for i in range(V):
			for adjNode in adj[i]:
				res[i].append(adjNode)
		return res


class Solution:

	def printGraph(self, V, adj):
		for i in range(V):
			adj[i].sort()
			adj[i] = [i] + adj[i]
		return adj


class Solution:

	def printGraph(self, V, adj):
		b = []
		for i in range(len(adj)):
			adj[i].insert(0, i)
			b.append(adj[i])
		return b


class Solution:

	def printGraph(self, V, adj):
		graph = [[] for _ in range(V)]
		for i in range(V):
			if len(adj[i]) != 0:
				graph[i].append(i)
				for j in adj[i]:
					graph[i].append(j)
		return graph


class Solution:

	def printGraph(self, V, adj):
		ans = []
		for i in range(len(adj)):
			temp = adj[i]
			temp.insert(0, i)
			ans.append(temp)
		return ans


class Solution:

	def printGraph(self, V, adj):
		adjList = []
		for i in range(V):
			adj[i].insert(0, i)
			adjList.append(adj[i])
		return adjList


class Solution:

	def printGraph(self, V, adj):
		adjList = []
		for (i, v) in enumerate(adj):
			adjList.append([i] + v)
		return adjList


class Solution:

	def printGraph(self, V, adj):
		res = []
		for i in range(V):
			tmp = []
			tmp.append(i)
			for j in adj[i]:
				tmp.append(j)
			res.append(tmp)
		return res


class Solution:

	def printGraph(self, V, adj):
		for (i, val) in enumerate(adj):
			val.insert(0, i)
		return adj


class Solution:

	def printGraph(self, V, adj):
		for i in range(V):
			print(i, end='')
			for j in adj[i]:
				print('->', j, end='')
			print()
		return []


class Solution:

	def printGraph(self, V, adj):
		ls = []
		for i in range(V):
			lst = []
			lst.append(i)
			for j in adj[i]:
				lst.append(j)
			ls.append(lst)
		return ls


class Solution:

	def printGraph(self, V, adj):
		ans = []
		for i in range(V):
			row = []
			row.append(i)
			for j in range(len(adj[i])):
				row.append(adj[i][j])
			ans.append(row)
		return ans


class Solution:

	def printGraph(self, V, adj):
		c = 0
		for i in adj:
			i.insert(0, c)
			c += 1
		return adj


class Solution:

	def printGraph(self, V, adj):
		nlist = []
		for i in range(len(adj)):
			t = []
			t.append(i)
			for j in adj[i]:
				t.append(j)
			nlist.append(t)
		return nlist


from collections import defaultdict

class Solution:

	def printGraph(self, V, adj):
		a = defaultdict(list)
		for i in range(V):
			a[i].append(i)
			for j in adj[i]:
				a[i].append(j)
		return a


class Solution:

	def printGraph(self, V, adj_list):
		n = len(adj_list)
		clone = [[i] for i in range(n)]
		for i in range(n):
			for j in range(len(adj_list[i])):
				clone[i].append(adj_list[i][j])
		return clone


class Solution:

	def printGraph(self, V, adj):
		ans = []
		for v in range(V):
			li = []
			li.append(v)
			li.extend(adj[v])
			ans.append(li)
		return ans


from collections import defaultdict

class Solution:

	def printGraph(self, V, adj):
		for (u, v) in enumerate(adj):
			v.insert(0, u)
		return adj


class Solution:

	def printGraph(self, V, adj):
		L = []
		for i in range(len(adj)):
			holder = [i]
			for j in adj[i]:
				holder.append(j)
			L.append(holder)
		return L


class Solution:

	def printGraph(self, V, adj):
		for (i, j) in zip(range(V), adj):
			j.insert(0, i)
		return adj


class Solution:

	def printGraph(self, v, adj):
		for i in range(v):
			adj[i].insert(0, i)
		return adj


class Solution:

	def printGraph(self, V, adj):
		res = []
		for i in range(V):
			curr = [i]
			for nei in adj[i]:
				curr.append(nei)
			res.append(curr)
		return res


class Solution:

	def printGraph(self, V, adj):
		ans = []
		i = 0
		for ad in adj:
			a = ad
			ad.insert(0, i)
			ans.append(a)
			i += 1
		return ans